'Dynamic where condition LINQ

I have a problem with my code. I don't know how I can insert in my selection all equals conditions of:

codicielementipartizione.sezione == el[i].ToString()

dynamically from

codicielementipartizione.sezione == el[1].ToString()

to

codicielementipartizione.sezione == el[el.count - 1].ToString()

Tn this code:

var selection = (from codicielementipartizione inlistacodici.cep
                 where codicielementipartizione.uno == 1 && 
                       codicielementipartizione.sezione == el[i].ToString() 
                 select codicielementipartizione).ToList();


Solution 1:[1]

You can make the fixed part of the query which will be IQueryable. After that you can add your conditions as such.

Fixed part:

var query = from codicielementipartizione in listacodici.cep
            where codicielementipartizione.uno == 1;

Dynamic part:

foreach(var condition in el)
   query = query.Where(codicielementipartizione.sezione == el.ToString());

Query execution:

var result = query.Select().ToList();

Solution 2:[2]

Maybe you mean this:

var selection = (from codicielementipartizione in listacodici.cep
                 where codicielementipartizione.uno == 1 
                    && el.Select(i => i.ToString()).Contains(codicielementipartizione.sezione)
                 select codicielementipartizione).ToList();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert
Solution 2 Perseus