开发者

multiple table LINQ-SQL query using anonymous types

开发者 https://www.devze.com 2023-03-24 20:14 出处:网络
I\'m running ASP.NET MVC开发者_开发问答 + LINQ-to-SQL using .NET 4.0. I have query similar to this:

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" };
0

精彩评论

暂无评论...
验证码 换一张
取 消