实体框架核心中的IN子句

Kum*_*wal 6 entity-framework-core

我想将此SQL查询转换为Entity Framework Core 2.0查询。

SELECT * 
FROM Product
WHERE ProdID IN (1,2,3);
Run Code Online (Sandbox Code Playgroud)

Mar*_*dle 20

根据对该问题的评论,您在 EF Core 中执行此操作的方式与 LINQ-to-SQL 相同:Enumerable.ContainsWhere表达式中的数组使用扩展方法。

public async Task<List<Product>> GetProducts(params int[] ids)
{
    return await context.Products
        .Where(p => ids.Contains(p.ProdID)) // Enumerable.Contains extension method
        .ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)

请参阅相关的 LINQ to Entities 问题