I'm running ASP.NET MVC开发者_开发问答 + LINQ-to-SQL using .NET 4.0.
I have query similar to this:
var labels = (from c in db.Customers
join o in db.Orders
on c.CustID equals o.CustID
select o).Distinct();
Can I do the same with an anonymous query, something like this
Customers.Select(x => x.Orders)
??
How would I do this if my query depth is 3 or 4 tables deep?
The query you have there is more or less equivalent to this:
var labels = db.Customers
.Join(db.Orders, c => c.CustID, o => o.CustId, (c, o) => o)
.Distinct();
FYI, it can go by different names. "Lambda" syntax is a common one. "Fluent" syntax is another. There's nothing anonymous about it. Anonymous types on the other hand is an unnamed type defined like so:
var anonymousObject = new { SomeField = 2, SomeOtherField = "Foo" };
精彩评论