linq中group by后如何对其他列count

friwhibo 2013-07-25 08:52:57
 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这句怎么写?
};


==============
版主提醒: 下次提问时,请将你的代码格式化一下
...全文
473 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
friwhibo 2013-08-03
  • 打赏
  • 举报
回复
终于解决了,但是方法很搞笑!!!
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")==x.Field<string>("exam"))
                    };
friwhibo 2013-07-30
  • 打赏
  • 举报
回复
这个count是要计算,每个医生CT做了多少个,MR做了多少个。。。。
friwhibo 2013-07-30
  • 打赏
  • 举报
回复
这两个表是分别从两个数据库读出来的,没有专门去定义什么:这两个
数据库都是从单位导出样本acsess数据库,再导入到自己sql的,都是ntext的,允许null。

            LinkDataBase rvuLink = new LinkDataBase();
rvuDs = rvuLink.SelectDataBase(rvuSendStrSQL, rvuSendTableName, rvuSendConnectionString);
rvuTable = rvuDs.Tables[0];
LinkDataBase examLink1 = new LinkDataBase();
examReportDs = examLink1.SelectDataBase(examReportSendStrSQL, examSendTableName1, examSendConnectionString);
examReportTable = examReportDs.Tables[0];


样本数据是这样子的
因为select 用了as 所以代码里的列表有所不同

friwhibo 2013-07-30
  • 打赏
  • 举报
回复
无法将 lambda 表达式 转换为委托类型“System.Func<System.Data.DataRow,bool>”,原因是块中的某些返回类型不能隐式转换为委托返回类型 无法将类型“string”隐式转换为“bool”
q107770540 2013-07-30
  • 打赏
  • 举报
回复
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"))
                    };
q107770540 2013-07-30
  • 打赏
  • 举报
回复
给出你两个DataTable的定义结构和测试数据
friwhibo 2013-07-25
  • 打赏
  • 举报
回复
Convert.ToDecimal()确实解决了小数点的问题,谢谢,但count依然无法通过编译
friwhibo 2013-07-25
  • 打赏
  • 举报
回复
报错 错误 1 无法将 lambda 表达式 转换为委托类型“System.Func<System.Data.DataRow,bool>”,原因是块中的某些返回类型不能隐式转换为委托返回类型 错误 2 无法将类型“string”隐式转换为“bool”
EnForGrass 2013-07-25
  • 打赏
  • 举报
回复
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()
friwhibo 2013-07-25
  • 打赏
  • 举报
回复
还有字符串转换为double有没有其他方法,我的转换结果中会出现:3.99999999999之类的情况,实际上最多小数点后1位

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧