我有一个属于区域的对象Invoice:
public class Invoice : IEntity, IValidatableObject
{
...
public virtual int? AreaId { get; set; }
...
// Navigation properties
public virtual Area Area { get; set; }
...
}
我还有一个可以属于多个区域的User对象:
public class User : IEntity, INamedType
{
...
//Navigation properties
public virtual ICollection<Area> Areas { get; set; }
...
}
我想要实现的是为用户提供属于他们任何区域的所有发票。我试着写它,但它甚至没有接近正确:
var invoices = _db.Invoices.Where(x => x.AreaId == user.Areas.Contains(z => z.Id));
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
var invoices = _db.Invoices.Where(x => user.Areas.Any(z => z.Id == x.AreaId));
答案 1 :(得分:0)
我觉得这样的事情应该有效:
var invoices = _db.Invoices.Where(x => user.Areas.Any(z => z.ID == x.AreaId))