Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where IN Clause in Entity Framework Core

I want to convert this SQL query to an Entity Framework Core 2.0 query.

SELECT * 
FROM Product
WHERE ProdID IN (1,2,3);
like image 209
Kumudini Porwal Avatar asked Aug 10 '18 12:08

Kumudini Porwal


1 Answers

As per the comments on the question, the way you do this in EF Core is the same as for LINQ-to-SQL: use the Enumerable.Contains extension method on an array in your Where expression.

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

See related LINQ to Entities question

like image 178
Mark Rendle Avatar answered Oct 19 '22 05:10

Mark Rendle