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

时间:2018-11-24 17:32:16

标签: c# algorithm listbox

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

在右侧的listbox中,您可以看到:employeeidquestionidscore。在右边的listbox中,通过按total average score,我希望每个employeeid的{​​{1}}。我需要制作一个button,它使用algorithm的正确值。 我怎样才能做到这一点?我不知道如何说我只想要listbox中的某些值(listboxemployeeid,而不是score)。

我正在使用questionid加载数据:

class

在while循环中,我使用了另一个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; }

class

在我的class ScoreMdw { private int employeeid; private int questionid; private int score; public ScoreMdw(int nr, int id, int s) { this.employeeid= nr; this.questionid= id; this.score = s; } public int EmployeeId { get { return employeeid; } } public int QuestionId { get { return questionid; } } public int Score { get { return score; } } public override string ToString() { string s = string.Format("{0} \t{1} \t{2}", this.employeeid, this.questionid, this.score); return s; } } 中,我正在这样做:

main window

2 个答案:

答案 0 :(得分:1)

您需要一个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是目标列表框的列

答案 1 :(得分:0)

        // 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);
        }