我有两个实体:
public class Banner {
public Int32 Id { get; set; }
public String Name { get; set; }
public virtual ICollection<Role> Roles { get; set; }
} // Banner
public class Role {
public Int32 Id { get; set; }
public String Name { get; set; }
public virtual ICollection<Banner> Banners { get; set; }
} // Role
我有一个角色ID列表:
List<Int32> roles;
我需要找到包含列表中至少一个角色的所有横幅:
context.Banners.Where(x => x.Roles.Contains(????))
我该怎么做?
谢谢你, 米格尔
答案 0 :(得分:1)
context.Roles.Where(r => roles.Contains(r.Id)).SelectMany(r=>r.Banners)
答案 1 :(得分:1)
你很亲密:
context.Banners.Where(x => x.Roles.Any(r => roles.Contains(r.Id)));
答案 2 :(得分:0)
您需要运行嵌套的Anys:
context.Banners.Where(x => roles.Any(roleId => x.Roles.Any(BannerRole => roleId == BannerRole.Id)