C# EF Core - 查询必须包含列表中的所有内容<object>

问题描述 投票:0回答:1
class OrderItem
{
  public long Id,
  public int Qty,
  public long ProductId
}

class Order
{
  public long Id
  public ICollectable<OrderItem> Items = new List<OrderItem>
}

var dtos = new List<ItemDTO>
{
  new ItemDTO { ProductId = 1, Qty = 2 },
  new ItemDTO { ProductId = 1, Qty = 1 }
};

var orders = await _context.Orders
    .Where(_ => _.Items.Any(i => dtos.All(dto => dto.ProductId == i.ProductId && dto.Qty <= i.Qty)));

如何从

Order
列表中查询dtos中所有项目
符合条件
的每个
Items

我无法在 LINQ to SQL 中将

.All()
与复杂对象一起使用。

c# entity-framework entity-framework-core linq-to-sql
1个回答
0
投票

我通过构建

Where
查询解决了我的问题。

它将填充许多

AND
链。

var whereQuery = _context.Orders.AsQueryable();

foreach(var dto in dtos)
{
  whereQuery = whereQuery.Where(x => x.Items.Any(i => i.productId == dto.productId && i.Qty >= dto.Qty);
}

var result = await whereQuery.ToListAsync();
© www.soinside.com 2019 - 2024. All rights reserved.