使用 LINQ 按 IList 的内容过滤 DbSet
Filter a DbSet by the content of an IList using LINQ
我有 2 个 ILists 供应商和 VwSrmAhmSuppliers。它们都是从数据库中查询的。我首先填写供应商。然后,当我查询 VwSrmAhmSuppliers 时,我想根据我已经在 Suppliers 中提取的内容过滤结果。
public IList<Supplier> Suppliers { get;set; }
public IList<Models.ExternalData.VwSrmAhmSupplier> VwSrmAhmSuppliers { get; set; }
public async Task OnGetAsync(Boolean? All)
{
//don't show all records unless explicity asked to!
if (All == true)
{
Suppliers = await _context.Supplier
.Include(s => s.Status)
.Include(c => c.Category)
.Include(c => c.Comments)
.OrderByDescending(c => c.CreateDate)
.ToListAsync();
//these do not work
//VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers.Where(d => Suppliers.Any(s=>s.SupplierNo == d.AhmSupplierNo)).ToListAsync();
//VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers.Where(v => Suppliers.Any(s=> s.SupplierNo.Equals(v.AhmSupplierNo))).ToListAsync();
//This does work, it gets all suppliers but it's too many
//VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers.ToListAsync();
VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
.Where(v => Suppliers
.Any(s => s.SupplierNo == v.AhmSupplierNo))
.ToListAsync();
}
}
产生的错误是:
InvalidOperationException: The LINQ expression
'DbSet .Where(v => __Suppliers_0 .Any(s =>
s.SupplierNo == v.AhmSupplierNo))' could not be translated. Either
rewrite the query in a form that can be translated, or switch to
client evaluation explicitly by inserting a call to either
AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我也不清楚。
您需要先投影出简单引用类型的内存集合(int
、string
等),而不是类型列表 Supplier
,然后使用对于您的 Any
或 Contains
条件,例如:
Suppliers = await _context.Supplier
.Include(s => s.Status)
.Include(c => c.Category)
.Include(c => c.Comments)
.OrderByDescending(c => c.CreateDate)
.ToListAsync();
//Project out the required references
var supplierNos = Suppliers.Select(s => s.SupplierNo).ToList();
//Use the simple reference type collection in your query
VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
.Where(d => supplierNos.Any(s=> s == d.AhmSupplierNo)).ToListAsync();
我有 2 个 ILists 供应商和 VwSrmAhmSuppliers。它们都是从数据库中查询的。我首先填写供应商。然后,当我查询 VwSrmAhmSuppliers 时,我想根据我已经在 Suppliers 中提取的内容过滤结果。
public IList<Supplier> Suppliers { get;set; }
public IList<Models.ExternalData.VwSrmAhmSupplier> VwSrmAhmSuppliers { get; set; }
public async Task OnGetAsync(Boolean? All)
{
//don't show all records unless explicity asked to!
if (All == true)
{
Suppliers = await _context.Supplier
.Include(s => s.Status)
.Include(c => c.Category)
.Include(c => c.Comments)
.OrderByDescending(c => c.CreateDate)
.ToListAsync();
//these do not work
//VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers.Where(d => Suppliers.Any(s=>s.SupplierNo == d.AhmSupplierNo)).ToListAsync();
//VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers.Where(v => Suppliers.Any(s=> s.SupplierNo.Equals(v.AhmSupplierNo))).ToListAsync();
//This does work, it gets all suppliers but it's too many
//VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers.ToListAsync();
VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
.Where(v => Suppliers
.Any(s => s.SupplierNo == v.AhmSupplierNo))
.ToListAsync();
}
}
产生的错误是:
InvalidOperationException: The LINQ expression 'DbSet .Where(v => __Suppliers_0 .Any(s => s.SupplierNo == v.AhmSupplierNo))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我也不清楚。
您需要先投影出简单引用类型的内存集合(int
、string
等),而不是类型列表 Supplier
,然后使用对于您的 Any
或 Contains
条件,例如:
Suppliers = await _context.Supplier
.Include(s => s.Status)
.Include(c => c.Category)
.Include(c => c.Comments)
.OrderByDescending(c => c.CreateDate)
.ToListAsync();
//Project out the required references
var supplierNos = Suppliers.Select(s => s.SupplierNo).ToList();
//Use the simple reference type collection in your query
VwSrmAhmSuppliers = await _externalcontext.VwSrmAhmSuppliers
.Where(d => supplierNos.Any(s=> s == d.AhmSupplierNo)).ToListAsync();