我有 Product 实体并在存储库类中编写一个方法(使用 npgsql)。但我收到错误:
无法翻译。以可翻译的形式重写查询,或者通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用来显式切换到客户端计算。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038 。System.InvalidOperationException:LINQ 表达式的 => s.ProductId == EntityShaperExpression:
public class Product:IEntity
{
public string Id { get; set; }
public string SerialNo { get; set; }
public string Imei { get; set; }
public string Name { get; set; }
}
public class ProductInput
{
public string ProductId { get; set; }
public string SerialNo { get; set; }
public string Imei { get; set; }
}
public async Task<List<Product>> GetProducts(List<ProductInput> input) //EfCoreProductRepository.cs
{
var result = (from product in (await GetDbContextAsync()).Products
where input.Any(s => s.ProductId == product.Id && s.SerialNo == product.SerialNo && s.Imei == product.Imei)
select product).ToList();
return result;
}
我应该如何编辑查询以避免出现此错误?
看起来您正在尝试检索
input
按多个条件与您的行匹配的数据库行。有多种方法可以做到这一点,例如:input
数据推入表值 (SQL Server) 或类型数组(Postgres) 参数,甚至 JSON 字符串 blob (可怕) 并INNER JOIN
使用该参数执行操作。WHERE
零件中所需的匹配项。input
在作者看来,选项 2(动态构建的 Linq 查询)是这里总体上“最好的”,所以这样做:
首先,get
PredicateBuilder
- 它是一个非常重要的依赖项,使得使用 Linq 构建Or
谓词变得更加容易。它实际上是class
从 Albahari 网站上复制粘贴的。其次,像这样使用它:
谓词生成器
为了后代,我
class PredicateBuilder
在这里转载: