有人可以帮助我完成这项工作吗?
我有以下代码:
dt.Columns.Add("Field1", typeof(String));
dt.Columns.Add("Field2", typeof(String));
dt.Columns.Add("NR", typeof(Double));
dt.Rows.Add("A", "990001", 100);
dt.Rows.Add("A", "990001", 120);
dt.Rows.Add("B", "990001", 200);
dt.Rows.Add("B", "990001", 300);
var fields = new List<string>() { "Field1", "Field2" };
var q = dt.AsEnumerable().AsQueryable()
.GroupBy("new(" + qfields + ")", "it")
.Select("new(Key as Group, Sum(NR) as Tot)");
当我执行此代码时,我收到错误:
“DataRow”类型中没有属性或字段“NR”
我需要结果为:
A, 990001, 220
B, 990001, 500
答案 0 :(得分:1)
var q =
dt.AsEnumerable()
.AsQueryable()
.GroupBy("new(it[\"Field1\"] as Field1,it[\"Field2\"]as Field2)", "it")
.Select("new(Key as Group, Sum(double(it[\"NR\"])) as Tot)");
foreach (var v in q)
{
Console.WriteLine(v);
}