使用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是成员,要么不是成员。
在用户不是成员的项目中,我将RoleId
和UserId
设置为null
。
基本上我认为这将是一次左联接...
如何使用Linq to Entities中的lambda表达式来完成此操作?