Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态LINQ到数据表:为什么使用硬代码字符串;它";作为数据行_C#_Linq_Dynamic Linq - Fatal编程技术网

C# 动态LINQ到数据表:为什么使用硬代码字符串;它";作为数据行

C# 动态LINQ到数据表:为什么使用硬代码字符串;它";作为数据行,c#,linq,dynamic-linq,C#,Linq,Dynamic Linq,假设我有一个包含三列的DataTable,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"); 我使用

假设我有一个包含三列的
DataTable
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");
我使用
GroupBy
columns
C1
C2
像下面的代码一样,它工作得非常好:

 var output = dt.AsEnumerable()
            .AsQueryable()
            .GroupBy("new(it[\"C1\"] as C1, it[\"C2\"] as C2)", "it");
如果您注意到,有一个硬代码字符串
it
表示为
DataRow
,我从以下方面了解到这一点:

。如果我尝试将
it
更改为string
row
,例如:

GroupBy("new(row[\"C1\"] as C1, row[\"C2\"] as C2)", "row");
我将获得运行时错误:

类型“DataRow”中不存在属性或字段“row”


所以,我不太清楚为什么必须将
硬编码为
数据行
?这有什么原因吗。

我想他们必须调用枚举中的当前对象“something”,因此“it”似乎是一个合理的选择。

动态LINQ提供程序就是这样编写的。它有三个硬编码关键字,当它试图解析LINQ表达式时可以识别它们:
It
iff
、和
new

static readonly string keywordIt = "it";
static readonly string keywordIif = "iif";
static readonly string keywordNew = "new";
如果您想查看更多详细信息,可以自己检查到LINQ程序集的


(顺便说一句,这里的“it”似乎是“identifier token”的缩写,如果我不得不猜测的话。)

是的,因为动态LINQ字符串被编译成Lambda表达式,并且需要输入参数。由于您无法指定输入参数的调用方式,因此它们只使用关键字“it”来引用可枚举的元素。换句话说,
Select(“…”
调用被编译成
Select(it=>…)