我使用Linq查询获取月份和年份数值,我的查询是:
brgData.Where(t => t.reportingDate != null && t.reportingDate.Value.Month == month4 && t.reportingDate.Value.Year == month4Year).Sum(t => t.TranCount)
我想使用dataSet.Tables查询实现相同的逻辑,但是我收到错误:
无法解析符号'Value'。
以下是我的询问:
var transactionMnth4 = dataSet.Tables["dataBridge"]
.AsEnumerable()
.Where(a => Convert.ToDateTime(a["reportingDate"].ToString()) != null && Convert.ToDateTime(a["reportingDate"].ToString()).Value.Month == month4 && Convert.ToDateTime(a["reportingDate"].ToString()).Value.Year == month4Year)
.GroupBy(a => 1)
.Select(d =>
d.Sum(a => a.Field<double?>("TranCount"))
).FirstOrDefault();
答案 0 :(得分:1)
删除.Value
,DateTime
不是Nullable<DateTime>
var transactionMnth4 = dataSet.Tables["dataBridge"]
.AsEnumerable()
.Where(a => a["reportingDate"].ToString() != null
&& Convert.ToDateTime(a["reportingDate"].ToString()).Month == month4
&& Convert.ToDateTime(a["reportingDate"].ToString()).Year == month4Year)
.GroupBy(a => 1)
.Select(d =>
d.Sum(a => a.Field<double?>("TranCount"))
).FirstOrDefault();
但是如果该列实际上是一个可以为空的DateTime
列,则首先不需要将其转换为字符串。然后使用Field
扩展方法:
var transactionMnth4 = dataSet.Tables["dataBridge"]
.AsEnumerable()
.Where(a => a.Field<DateTime?>("reportingDate").HasValue
&& a.Field<DateTime?>("reportingDate").Value.Month == month4
&& a.Field<DateTime?>("reportingDate").Value.Year == month4Year)
.GroupBy(a => 1)
.Select(d => d.Sum(a => a.Field<double?>("TranCount")))
.FirstOrDefault();
(不知道你在那里总结了什么)