how to use LINQ and EntityFramework to create a join

asp.net c# entity-framework linq

Question

I'm attempting to write the following sql query using linq 2 in EF 4.0.

SELECT * FROM Role
LEFT JOIN Queue 
ON Role.RoleId = Queue.RoleId 
WHERE QueueId = 361

In EF 4.0, how might I do this?

1
2
4/9/2012 5:08:36 PM

Accepted Answer

The navigation attributes that are loaded when you acquire the entity are often used for this, however you may also use the following:

from r in Roles
from q in Queues
where r.RoleId == q.RoleId
where q.QueueId == 361
select new { r.RoleId, q.QueueId /*other bits you want*/}
4
4/9/2012 6:28:34 AM

Popular Answer

I hope the following may be helpful.

I'd advise doing additional research on joins.

var result=(from p in Roles
join pa in Queue on p.RoleId equals pa.RoleId into temproles
from addresses in temproles.DefaultIfEmpty() where temproles.queueId = 361
select new { p, pa} );


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