我有一个返回摘要表的LINQ函数。
private DataTable CreateSummaryFocusOfEffortData()
{
var result = ReportData.AsEnumerable()
.GroupBy(row => new
{
Value = row.Field<string>("Value"),
Description = row.Field<string>("Description")
})
.Select(g =>
{
var row = g.First();
row.SetField("Hours", g.Sum(r => r.Field<double>("Hours")));
return row;
});
return result.CopyToDataTable();
}
现在我想在这个函数中添加一个WHERE子句,这样它只能为列表中的字段汇总行。类似于sql中的IN运算符。例如:假设我有一个带有值(1,2,3)的列表,我希望将where子句与列表中的值相对应。
答案 0 :(得分:1)
只是一个如何尝试它的例子:
private DataTable CreateSummaryFocusOfEffortData(List<int> yourList)
{
var result = ReportData.AsEnumerable()
.GroupBy(row => new
{
Value = row.Field<string>("Value"),
Description = row.Field<string>("Description")
})
.Select(g =>
{
var row = g.First();
row.SetField("Hours",
g.Where(r=>yourList.Contains(r.Field<int>("Id")))
.Sum(r => r.Field<double>("Hours")));
return row;
});
return result.CopyToDataTable();
}