使用Linq将十进制字段求和到数据表

时间:2018-12-15 21:22:51

标签: c# linq sum linq-to-entities decimal

经过很多挫折后,我能够使它正常工作,但是我希望有人可以向我解释为什么这行得通:

var monthCharges = WRVUData.AsEnumerable()
                               .Where(dr => dr.Field<DateTime>("Posting_Month") == new DateTime(month.Year, i, 1))
                               .Where(dr => dr.Field<string>("Report_Producer_Name") == provider || dr.Field<string>("Group_Name") == group)
                               .Where(dr => dr.Field<decimal?>("Charges") != null)
                               .Sum(dr => dr.Field<decimal>("Charges"));

但这不是:

var monthCharges = (from r in WRVUData.AsDataView()
                 select new
                 {
                     charges = r.Field<decimal?>("Charges"),
                     activityType = r.Field<string>("Activity_Type"),
                     postingMonth = r.Field<DateTime>("Posting_Month"),
                     provider = r.Field<string>("Report_Producer_Name"),
                     groupName = r.Field<string>("Group_Name"),
                     WRVUs = r.Field<decimal>("W_RVUs")
                 })
                                    .Where(dr => dr.postingMonth == new DateTime(month.Year, i, 1))
                                    .Where(dr => dr.provider == provider || dr.groupName == group)
                                    .Where(dr => dr.charges != null)
                                    .Sum(dr => dr.charges);

第二个版本引发“指定的转换无效”错误。这两个基本上不是同一回事吗?

0 个答案:

没有答案