Thursday, July 28, 2011

Linq to Entity outer joins

Just spend a large amount of time trying to do a simple Linq to entity statement which in SQL would have taken me 5 minutes. What I tried to do, is left outer joins on entities that could not be placed on the left hand side of the equals and I got the following error:

the name '' is not in scope on the left hand side of equals
What you need to do is the following:
join users in entities.Users on new { ID = userroles.fkUserId } equals new { ID = users.ID } into users_join  from users in users_join.DefaultIfEmpty()
To place the entity on the left you need to declare "on new { ID = userroles.fkUserId } equals new { ID = users.ID }", so basically swapping the values around!

Hope it helps.

No comments: