Does LINQ to SQL support t-sql "in"

I don’t know how to better describe the name (so feel free to change), but essntial I want to create equivalent of the following statement in LINQ to SQL:

select * from products where category in ('shoes','boots')

Fields will come from the query string essentially (more securely than this, but for convenience of explanation) i.e.

string[] myfields = request.querystring["categories"].split(',');

thanks

+3
source share
3 answers

Yes, you are using Contains:

db.Products.Where(product => myFields.Contains(product.Category))

aka

from product in db.Products
where myFields.Contains(product.Category)
select product
+15
source

, , .Contains. , Bing ( ): Linq-To-Entities .Contains . :
http://george.tsiokos.com/posts/2007/11/30/linq-where-x-in/

+1

In theory, you would use:

var productList = from product in context.Products
    where myfields.contains(product.category)
    select product

However, you need to test this - it seems that I remember that there was an error in the Linq2Sql optimizer when I tried to use the List <> values ​​and the arrays used in this way (perhaps this only happened if you tried to execute them as IQueryable)

0
source

Source: https://habr.com/ru/post/1715535/


All Articles