Evgeniy Labunskiy
Evgeniy Labunskiy

Reputation: 2042

LINQ request with LEFT JOIN return not expected result

Please help me to convert this query to Linq to Entities.

SELECT 
    d.`DepartmentId`,
    d.`ParentDepartmentId`,
    d.`LeftKey`,
    d.`RightKey`,
    udp.`UserId`,
    udp.`IsAllowed` 
FROM
    Departments AS d 
    LEFT JOIN UserDepartmentPermissions AS udp 
        ON udp.`DepartmentId` = d.`DepartmentId`
WHERE d.`IsExcluded` = 0

I'm tring this:

(from d in Departments
   join udp in UserDepartmentPermissions 
   on d.DepartmentId equals udp.DepartmentId into JoindedDepsUserPermissions
   from udp in JoindedDepsUserPermissions.DefaultIfEmpty()
   where d.IsExcluded == 0
   select  new
     {
      d.DepartmentId,
      d.ParentDepartmentId,
      d.LeftKey,
      d.RightKey,
      udp.UserId,
      udp.IsAllowed
     }).ToList();

But it's not return the same result...

Can anybody help?

Upvotes: 0

Views: 69

Answers (1)

Eranga
Eranga

Reputation: 32437

Try

var result = Deparments.Where(d => d.IsExcluded == 0)
             .Select(d => new {
                   d.DepartmentId,
                   d.ParentDepartmentId,
                   d.LeftKey,
                   d.RightKey,
                   d.Permissions
                 });

Use the navigational property in Department to access the Permissions

Upvotes: 1

Related Questions