0

I am new to joins in linq to entities. I have one query in sql server and want to convert it into LINQ to Entities.

Can someone please provide the solution for the same? Do we have any online tool to convert sql queries to LINQ to entities?

 SELECT  R.ID,r.Name,u.UserId
      FROM Roles R
      Left JOIN UserRoles U ON r.Id = u.RoleId 
      AND   [UserId] = '5'
      where  [UserId] IS NULL 
1
  • You should take a look at linqpad. This is a great tool to experiment with! I have been using it for years now... it will help you to learn linq. Commented Sep 29, 2016 at 13:26

2 Answers 2

1

DefaultIfEmpty will result in a left outer join, therefore as you want a simple left join you should do as follows:

var list = (from r in context.Roles
    join u in context.UsersRoles on r.Id equals u.RoleId && u.UserId='5' into x
    where r.UserId == null
    select new
    {
        r.Id,
        r.Name,
        u.UserId
    }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

0
var list = (from r in context.Roles
    join ur in context.UsersRoles on r.Id equals ur.RoleId && ur.UserId='5' into x
    from y in x.DefaultIfEmpty()        
    where r.UserId ==null
    select new Object
    {
        Id = r.Id,
        Name = r.Name,
        ur.UserId
    }).ToList();

Note: Not understand your second UserId IS NULL logic

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.