Should I use lambda or LINQ for a multi-table join in Entity Framework 4.1?

asp.net-mvc entity-framework lambda linq sql

Question

I'm attempting to convert a multi table join to utilize the Entity Framework since I'm new to it. The first SQL had a similar appearance.

JOIN tab_NewsCommunities S1 ON S1.News_ID = tab_News.NewsID
inner join tab_communities com on S1.Community_ID = com.Community_ID and com.RecordStatus_ID = 2
JOIN tab_Communities c ON S1.Community_ID = c.Community_ID
JOIN tab_Favorites uf ON S1.Community_ID = uf2.Community_ID
        WHERE uf.[User_ID] = @UserId 

This is the general appearance of the new Entity model after some simplification. Can somebody assist me with the SQL to LINQ or lambda conversion? Although there are many online examples, they are all single table joins. Thanks

entity model

1
4
12/20/2011 4:08:39 AM

Accepted Answer

You should read this blog entry by CRAIG STUNTZ, in my opinion. The concept is that you may choose all of your properties using the navigation link without using the JOIN operator as long as your model has the relevant relationships. Since your model can do it, why do it yourself? This is the true beauty of LINQ.

4
6/23/2011 9:11:45 PM

Popular Answer

In the Include method of the most recent Entity Framework, a lamda expression is supported. You may then use a second choice to go further deeper into the connection.

var result = context.News.Include(x=>x.NewsCommunity.select(z=>z.Communities)).Where(...)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow