实体框架核心中的左联接

时间:2020-02-18 12:57:10

标签: entity-framework-core linq-to-entities entity-framework-core-3.0 entity-framework-core-3.1

使用Entity Framework Core 3.1,我具有以下实体:

public class Project {
   public Int32 ProjectId { get; set; }
   public ICollection<Member> Members { get; set; }
}

public class Role {
   public Int32 RoleId { get; set; }
   public ICollection<Member> Members { get; set; }
}

public class User {
   public Int32 UserId { get; set; }
   public ICollection<Member> Members { get; set; }
}

public class Member {
   public Int32 ProjectId { get; set; }
   public Int32 UserId { get; set; }
   public Int32 RoleId { get; set; }
   public Project Project { get; set; }
   public User User { get; set; }
   public Role Role { get; set; }
}

我正在获得UserId=1用户所在的项目:

projects
  .SelectMany(y => y.Members, (Project, Member) => new { Project, Member }) 
  .Where(y => y.Member.UserId == 1)
  .Select(y => new {
    ProjectId = y.ProjectId,
    RoleId = y.Member.RoleId,
    UserId = y.Member.UserId
  });

但是我需要获取所有项目,要么User是成员,要么不是成员。

在用户不是成员的项目中,我将RoleIdUserId设置为null

基本上我认为这将是一次左联接...

如何使用Linq to Entities中的lambda表达式来完成此操作?

0 个答案:

没有答案