假设我有一个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");
我在 nuget to GroupBycolumns上使用 Dynamic Linq 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?这有什么原因吗。