Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个列表框计算列表框中的总平均值_C#_Algorithm_Listbox - Fatal编程技术网

C# 从另一个列表框计算列表框中的总平均值

C# 从另一个列表框计算列表框中的总平均值,c#,algorithm,listbox,C#,Algorithm,Listbox,我在左侧列表框上方的列表框中有这些值,您可以看到表的标题,但这是荷兰语: 在右边的列表框中,您可以看到:employeeid、questionid和score。在右边的列表框中,我想通过按下一个按钮,得到每个员工ID的平均总分。我需要做一个算法,它取列表框的正确值。 我该怎么做?我不知道怎么说我只想要列表框employeeid和score中的某些值,而不是questionid 我正在使用一个类加载数据: public List<ScoreMdw> GetScoreMdwList()

我在左侧列表框上方的列表框中有这些值,您可以看到表的标题,但这是荷兰语:

在右边的列表框中,您可以看到:employeeid、questionid和score。在右边的列表框中,我想通过按下一个按钮,得到每个员工ID的平均总分。我需要做一个算法,它取列表框的正确值。 我该怎么做?我不知道怎么说我只想要列表框employeeid和score中的某些值,而不是questionid

我正在使用一个类加载数据:

public List<ScoreMdw> GetScoreMdwList()
        {
            List<ScoreMdw> scoremdwList = new List<ScoreMdw>();
            conn.Open();
            string query = ("Select employeeid, questionid, score from contentment");
            SqlCommand cmd = new SqlCommand(query, conn);

        try
        {
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    ScoreMdw sm = new ScoreMdw((int)dr["employeeid"], (int)dr["questionid"], (int)dr["score"]);
                    scoremdwList.Add(sm);
                }
            }

        }
        catch (Exception ex)
        {
            Exception error = new Exception("error", ex);
            throw error;
        }

        finally
        {
            conn.Close();
        }

        return scoremdwList;
    }
在我的主窗口中,我正在执行以下操作:

private void btnLoadScores_Click(object sender, RoutedEventArgs e)
        {
            scoremdwList = new List<ScoreMdw>();

        try
        {
            conn.Open();

            List<string> headers = so.GetContentmentHeaders();

            foreach (string header in headers)
                txtHeader.Text += header + "\t";

            scoremdwList = so.GetScoreMdwList();
            lbScores.ItemsSource = scoremdwList;
        }

        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        finally
        {
            conn.Close();
        }
    }

您需要一个linq查询来汇总相同员工id的分数,如

lbScores.ItemsSource = (from e in scoremdwList
    group e by e.EmployeeId into grp
    select new 
    {
        EmployeeId = grp.Key,
        TotalScore = grp.Sum(a => a.Score)
    }).ToList();

其中EmployeeId和TotalScore是目标列表框的列

需要更多代码!发布您试图计算它的代码,或者至少发布按钮单击的事件处理程序和填充另一个列表框的代码。与其设置自己必须解析字符串,您可以使用数据绑定在列表框中显示一个列表,并获取所需的属性want@JLe刚刚添加了我的代码。这是每个员工的平均总分吗?你能评论一下你的代码吗?谢谢。这是每位员工的平均总分。我会在以后评论我等你评论:我添加了评论。我不想用一个好的算法来处理查询等。
        // string: employeeid - first int: total score - second int: number of questions
        Dictionary<string, Tuple<int, int>> results = new Dictionary<string, Tuple<int, int>>();
        foreach (ListViewItem item in lstvwSource.Items)
        {
            // check if employeeid is in Dictionary
            if (results.ContainsKey(item.Text))
            {
                // if employeeid exists in dictionary
                // add currnet score to total score
                // and add one to number of questions
                results[item.Text] = new Tuple<int, int>(Convert.ToInt32(item.SubItems[1].Text) + results[item.Text].Item1, +results[item.Text].Item2 + 1);
            }
            else
            {
                // if employeeid does not exist in dictionary
                // add employeeid , score of the question
                // set number of questions to 1
                Tuple<int, int> tuple = new Tuple<int, int>(Convert.ToInt32(item.SubItems[1].Text), 1);
                results.Add(item.Text, tuple);
            }
        }
        // 
        lstvwDest.Items.Clear();
        foreach (var result in results)
        {
            ListViewItem newItem = new ListViewItem();
            newItem.Text = result.Key; // employeeid
            newItem.SubItems.Add(result.Value.Item1.ToString()); // total score
            double avg = (double)result.Value.Item1 / (double)result.Value.Item2;
            newItem.SubItems.Add(avg.ToString()); // average score
            lstvwDest.Items.Add(newItem);
        }
        // string: employeeid - first int: total score - second int: number of questions
        Dictionary<string, Tuple<int, int>> results = new Dictionary<string, Tuple<int, int>>();
        foreach (ListViewItem item in lstvwSource.Items)
        {
            // check if employeeid is in Dictionary
            if (results.ContainsKey(item.Text))
            {
                // if employeeid exists in dictionary
                // add currnet score to total score
                // and add one to number of questions
                results[item.Text] = new Tuple<int, int>(Convert.ToInt32(item.SubItems[1].Text) + results[item.Text].Item1, +results[item.Text].Item2 + 1);
            }
            else
            {
                // if employeeid does not exist in dictionary
                // add employeeid , score of the question
                // set number of questions to 1
                Tuple<int, int> tuple = new Tuple<int, int>(Convert.ToInt32(item.SubItems[1].Text), 1);
                results.Add(item.Text, tuple);
            }
        }
        // 
        lstvwDest.Items.Clear();
        foreach (var result in results)
        {
            ListViewItem newItem = new ListViewItem();
            newItem.Text = result.Key; // employeeid
            newItem.SubItems.Add(result.Value.Item1.ToString()); // total score
            double avg = (double)result.Value.Item1 / (double)result.Value.Item2;
            newItem.SubItems.Add(avg.ToString()); // average score
            lstvwDest.Items.Add(newItem);
        }