2

I am trying to create a linq 2 sql in EF 4.0 query like following sql query.

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

So how could i do this in EF 4.0?

3
  • 1
    You mean Linq; not Linq to Sql. Commented Apr 9, 2012 at 6:34
  • @AndrewBarber: No, I'm pretty sure he meant LINQ to SQL. Or more accurately LINQ to Entities since he's mapping against EF. See here: msdn.microsoft.com/en-us/library/bb386964.aspx Commented Apr 9, 2012 at 6:36
  • 2
    @Mike LINQ to SQL is a completely different product from EF. It would be like saying he was talking about nHibernate. Commented Apr 9, 2012 at 6:40

2 Answers 2

4

Normally this is done using navigation properties which are loaded when you get the entity, however you can also do this with 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*/}
Sign up to request clarification or add additional context in comments.

1 Comment

@Ali sure does, from indicates a datasource avaliable to the query in linq. This is similar to the concept of a JOIN inside SQL itself, and when the linq query gets translated to SQL this will turn into a SQL JOIN. I like multiple from's in a query instead of a join statement as its much more readable
3

try the following i hope it helps

i would recommend searching more about 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} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.