我想使用数据表
在linq中运行以下SQL查询select
SUM(cast(Percentage as int))
,Subject
from tablename
where Student_ID = xxx
group by Subject_ID
我该怎么办?任何帮助将不胜感激
答案 0 :(得分:1)
使用Enumerable.GroupBy
,Sum
和匿名类型来存储结果:
var query = tablename.AsEnumerable()
.Where(row => row.Field<int>("Student_ID") == xxx)
.GroupBy(row => new { SubjectID = row.Field<int>("Subject_ID"), Subject = row.Field<string>("Subject") })
.Select(g => new
{
SumPercentage = g.Sum(r => r.Field<int>("Percentage")),
Subject = g.Key.Subject, SubjectID = g.Key.SubjectID
});
我不知道为什么你需要将Percentage
投射到int
,但我很确定你能得到它。