首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

linq中group by后怎么对其他列count

2013-08-09 
linq中group by后如何对其他列count本帖最后由 q107770540 于 2013-07-30 10:31:44 编辑var reportQuery

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这句怎么写?
                    };


==============
版主提醒: 下次提问时,请将你的代码格式化一下
[解决办法]
 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 = 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"))
                    };

热点排行