I was playing around with expression trees and various Linq syntax. I wrote the following:
using (NorthwindDataContext DB = new NorthwindDataContext())
{
DataLoadOptions dlo = new DataLoadOptions();
// Version 1
dlo.AssociateWith<Customer>(c => c.Orders.Where(o => o.OrderID < 10700).Select(o => o));
// Version 2
dlo.AssociateWith<Customer>(c => from o in c.Orders
where o.OrderID < 10700
select o);
}
The Version 1 method returns an error saying “The operator ‘Select’ is not supported in Subquery.”
While Version 2 runs just fine. From what I understand I am writing the exact same thing, but one is with the “dot” notation syntax and the other is query expression syntax.
Am I missing something here? Why the error on one but not the other “if” they are in fact the same query?
To expand on Daniel’s answer, the
select ois known as a degenerate query expression – and it’s removed by the C# compiler. So your query is translated to:Note that without the
whereclause, however, the compiler would still include theSelectcall, so:is translated to
From section 7.15.2.3 of the language spec: