LINQ 是否使用 DataRelations 来优化连接?LINQ、DataRelations

2023-09-06 05:04:53 作者:糟老头孑丶

我在任何地方都找不到这个问题的答案,在我开始使用 Reflector 浏览生成的代码之前,我认为值得一问:

I can't find the answer to this anywhere, and before I start pawing through generated code with Reflector I thought it'd be worth asking:

假设我对数据集中的数据表运行以下 LINQ 查询:

Suppose I have the following LINQ query run against DataTables in a DataSet:

var list = 
   from pr in parentTable.AsEnumerable()
   join cr in childTable.AsEnumerable() on cr.Field<int>("ParentID") equals pr.Field<int>("ID")
   where pr.Field<string>("Value") == "foo"
   select cr;

如果父表和子表之间存在使用所示关键字段的 DataRelation,LINQ 会使用它吗?也就是说,它会在父表中找到 Value 为foo"的行,然后调用 GetChildRows 来投影子行吗?

If there's a DataRelation between the parent table and the child table that uses the key fields shown, will LINQ use it? That is, will it find the rows in the parent table for which Value is "foo" and then call GetChildRows to project the child rows?

或者这是我必须明确指定的吗?(如果是这样,我该怎么做?)

Or is this something that I have to specify explicitly? (And if so, how do I do this?)

推荐答案

我不这么认为.在这种情况下,LINQ to Objects 可能只会将两侧视为常规可枚举对象,并手动进行连接(无需查看 DataRelation).

I don't think so. In this case, LINQ to Objects will probably just treat the two sides as regular enumerable objects, and do the join manually (without looking at the DataRelation).