【发布时间】:2020-07-01 15:35:29
【问题描述】:
我有 2 个 IList 供应商和 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:LINQ 表达式 'DbSet .Where(v => __Suppliers_0 .Any(s => s.SupplierNo == v.AhmSupplierNo))' 无法翻译。任何一个 以可以翻译的形式重写查询,或切换到 通过插入对任何一个的调用来显式地评估客户 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync()。看 https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。
我也不清楚。
【问题讨论】:
-
您需要先投影出一个 id 列表,而不是
Supplier,然后在您的Any或Contains条件中使用它。
标签: c# linq asp.net-core razor-pages