我有dataTables我想找到课程类似
的null专业化Specization course year
null IT 0
null IT 0
null Math 0
如果在LINQ中进行查询
var NospecList = (from r in dt.AsEnumerable()
where r.Field<decimal>("year") == 0 && r.Field<string>("Specization ") == null
group r by r.Field<string>("course"));
但在linq中找不到以下结果
IT -> 2
Math -> 1
我想要计算其专业化为null的所有课程
答案 0 :(得分:2)
var NospecList = (from r in dt.AsEnumerable()
where r.Field<decimal>("year") == 0 && r.Field<string>("Specization ") == null
group r by r.Field<string>("course") into g
select new {Course=g.Key, Count=g.Count()});
<强>结果:强>
IT -> 2
Math -> 1
更新:
如果您想在foreach
中使用它,只需:
foreach(var item in NospecList )
{
//item.Course
//item.Count
}
答案 1 :(得分:1)
您的查询缺少“选择”语句。试试这个。
var NospecList = (from r in dt.AsEnumerable()
where r.Field<decimal>("year") == 0 && r.Field<string>("Specization ") == null
group r by r.Field<string>("course") into grp
select new {Course = grp.Key, Count = grp.Count()});