Search code examples
c#linqjoindatasetdatarelation

Does LINQ use DataRelations to optimize joins?


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:

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;

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?)


Solution

  • 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).