我有一个班级人员,其中包含地址和电话列表作为以下代码。 在我的查询中,我想要一个人的列表,但不包括已删除的地址和电话,但总是返回所有地址和电话,即使它们标记为已删除。我怎么能用lambda过滤那些嵌套列表?
public class Person{
public int PersonId { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Phone> Phones{ get; set; }
public virtual Company Company { get; set; }
}
public class Address{
public int AddressId { get; set; }
public string Street { get; set; }
public bool Deleted { get; set; }
[ScriptIgnore]
public virtual Person Person { get; set; }
}
public class Phone{
public int PhoneId { get; set; }
public string Number{ get; set; }
public bool Deleted { get; set; }
[ScriptIgnore]
public virtual Person Person { get; set; }
}
return GetDbSet<Person>()
.Include("Address")
.Include("Phones")
.Where(i => i.Company.CompanyId == company.CompanyId)
.OrderByDescending(o => o.CreateTime).ToList();
答案 0 :(得分:0)
只需在Where
子句中添加条件即可排除已删除的地址和手机,如:
Where(i => i.Company.CompanyId == company.CompanyId &&
i.Address.Any(r=> !r.Deleted) &&
i.Phone.Any(r=> !r.Deleted))