SQL to Linq to Entities转换

时间:2011-12-09 16:17:49

标签: linq-to-entities

我无法将此SQL转换为Linq To Entities。有人可以解释我应该如何分解它?

select
    p.ID, p.Name, p.IsActive, ug.Name 
from 
    Projects as p 
inner join UserGroups as ug ON p.UserGroups_ID = ug.ID
where 
    p.UserGroups_ID IN (select UserGroups_ID from UsersToUserGroups where Users_ID = 1)
    and 
    p.IsDeleted = 0
order by 
    p.UserGroups_ID, 
    p.Name

由于

2 个答案:

答案 0 :(得分:0)

var results = from p in context.Projects
              join ug in context.UserGroups
                  on p.UserGroups_ID equals ug.ID
              where (from utug in context.UserToUserGroups
                     where utug.Users_ID == 1
                     select UserGroups_ID).Contains(p.UserGroups_ID)
                  && !p.IsDeleted
              order by p.UserGroups_ID, p.Name
              select new { p.ID, p.Name, p.IsActive, UserGroupName = ug.Name };

答案 1 :(得分:0)

(
from p in [context].Projects
join ug in [context].UserGroups
on p.UserGroups_ID equals ug.ID
where ug.Users.Any(x => x.ID.Equals(userId))
&& p.IsDeleted.Equals(false)
orderby p.UserGroups_ID, p.Name
select new
{
     ID = p.ID,
     Name = p.Name,
     IsActiveStatus = p.IsActive,
     GroupName = ug.Name
}
)