linq中group by后如何对其他列count
本帖最后由 q107770540 于 2013-07-30 10:31:44 编辑
var reportQuery = from a in examReportTable.AsEnumerable()
join b in rvuTable.AsEnumerable()
on new { exam = a.Field<string>("exam"), examtype = a.Field<string>("examtype") } equals new { exam = b.Field<string>("rvuName"), examtype = b.Field<string>("rvuType") }
group b by new { reportDoc = a.Field<string>("reportDoc"), examtype = a.Field<string>("examtype") } into c
select new
{
DocName = c.Key.reportDoc,
examType = c.Key.examtype,
reportDocRvu = c.Sum(o => double.Parse(o.Field<string>("reportRvu"))),
reportDocNum =//examReportTable中对“exam”计数count这句怎么写?
};
on new { exam = a.Field<string>("exam"), examtype = a.Field<string>("examtype") } equals new { exam = b.Field<string>("rvuName"), examtype = b.Field<string>("rvuType") }
group b by new { reportDoc = a.Field<string>("reportDoc"), examtype = a.Field<string>("examtype") } into c
select new
{
DocName = c.Key.reportDoc,
examType = c.Key.examtype,
reportDocRvu = c.Sum(o => double.Parse(o.Field<string>("reportRvu"))),
reportDocNum = c.Count(d=>d.Field<string>("exam"))
};
为什么reportRvu不定义成decimal类型呢,或者你试试Convert.ToDecimal()
[解决办法]
var reportQuery = from a in examReportTable.AsEnumerable()
join b in rvuTable.AsEnumerable()
on new { exam = a.Field<string>("exam"), examtype = a.Field<string>("examtype") } equals new { exam = b.Field<string>("rvuName"), examtype = b.Field<string>("rvuType") }
group new {a,b} by new { reportDoc = a.Field<string>("reportDoc"), examtype = a.Field<string>("examtype") } into c
select new
{
DocName = c.Key.reportDoc,
examType = c.Key.examtype,
reportDocRvu = c.Sum(o => Convert.ToDecimal(o.Field<string>("reportRvu"))),
reportDocNum =c.Count(x=>x.Field<string>("exam"))
};