c# - 从另一个列表框计算列表框中的总平均值
问题描述
我的列表框中有这些值(在左侧列表框上方,您可以看到表格的标题,但这是荷兰语):
在listbox
右侧,您会看到employeeid
:questionid
和score
。在listbox
右边我想要的total average score
每一个employeeid
,按一下button
。我需要制作一个algorithm
,它采用正确的值listbox
。我怎样才能做到这一点?我不知道我怎么能说我只想要listbox
( employeeid
and score
, and not questionid
) 中的某些值。
我正在使用 aclass
加载数据:
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;
}
在 while 循环中,我使用了另一个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
我这样做:
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();
}
}
解决方案
You need a linq query to sum up score of same employee id like
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();
Where EmployeeId and TotalScore are the columns of target listbox
推荐阅读
- javascript - 在赛普拉斯中保存变量
- java - Spring MVC @ModelAttribute 未填充 AJAX Post 请求
- c++ - ASIO io_service 在第二次 run() 调用时不处理后处理程序
- opengl - glsl 顶点着色器导致 INTCONSTANT 错误
- java - 加载数据并推送到 RecyclerView 未显示
- r - 为什么使用 plotstyle="ggplot" 时 qqcomp 函数中没有显示任何点?
- google-apps-script - 将 google App Script 部署为 Sheets Add-On 后菜单项未出现
- jenkins - Jenkins 无法访问 Sonarqube 本地主机
- asynchronous - 此语句在 dart 的 Future 类中意味着什么:FutureOr
函数(动态)onValue - android - 获取“连接在收到完整标题之前关闭从网络加载图像时