【发布时间】:2013-03-01 17:58:36
【问题描述】:
假设我有一个DataTable,其中包含三列C1、C2、C3:
var dt = new DataTable();
dt.Columns.Add("C1", typeof (int));
dt.Columns.Add("C2", typeof (string));
dt.Columns.Add("C3", typeof(string));
dt.Rows.Add(1, "1", "1");
dt.Rows.Add(1, "1", "2");
dt.Rows.Add(1, "2", "2");
我使用Dynamic Linq on nuget 到GroupBy 列C1 和C2 就像下面的代码一样,它工作得很好:
var output = dt.AsEnumerable()
.AsQueryable()
.GroupBy("new(it[\"C1\"] as C1, it[\"C2\"] as C2)", "it");
如果您注意到,有一个 硬编码字符串 it 表示为 DataRow,我从以下方面得到这个想法:
。如果我尝试将 it 更改为字符串 row 例如:
GroupBy("new(row[\"C1\"] as C1, row[\"C2\"] as C2)", "row");
我会得到运行时错误:
“DataRow”类型中不存在属性或字段“行”
所以,我不太清楚为什么必须硬编码 it 为 DataRow?这有什么原因吗?
【问题讨论】:
标签: c# linq dynamic-linq