How to create a dyamically lambda expression, to feed a Where.() on a List<CustomClass>

13 Views Asked by At

Hello i have a List of this custom class of mine (List<ClassePerLifoFifoGestioneInner>).

The class i built like this:

   public class ClassePerLifoFifoGestioneInner
   {

    public int Artico01S00_LifoFifo_NrMag { get; set; }

    public string Artico01S00_LifoFifo_CodiceArticolo { get; set; }
    
    public decimal Artico01S00_LifoFifo_Anno_Formazione { get; set; }

    public short Artico01S00_Stato { get; set; }

    public string Artico01S00_Categoria { get; set; } 
   
    public string Artico01S00_SubCategoria { get; set; }

    public string Artico01S00_Famiglia { get; set; }
    
    public string Artico01S00_Sub_Famiglia { get; set; }

    }

I have a windows form, with some lookupedits, textboxes, calcEdits etc; where I input or choose some values in and when i press a button to search in the list that i have i would like to filter it with those values directly in the where in a way that's dynamically created without having to hardcode the lambda expression directly in to the code.

Example: If I enter NrMag = 1 and CodiceArticolo = 00024005 on my form. I want to filter my List like this: LstCurrClassePerGestioneDettaglio.Where(x => x.Artico01S00_LifoFifo_NrMag == NrMag && x.Artico01S00_LifoFifo_CodiceArticolo == CodiceArticolo) in a way that is dyanamic, based on how many values(lookupedits, calcEdits, textboxes) I input in the form.

Another example would be: I entered NrMag = 1, CodiceArticolo = 00024005, Stato = true; LstCurrClassePerGestioneDettaglio.Where(x => x.Artico01S00_LifoFifo_NrMag == NrMag && x.Artico01S00_LifoFifo_CodiceArticolo == CodiceArticolo && x.Artico01S00_Stato == Stato);

The final goal is to have this expression dynamically created and slam it in to the Where of my query.

var expression = MethodThatIdontKnowHowToCreate();

LstCurrClassePerGestioneDettaglio = LstCurrClassePerGestioneDettaglio.Where(expression).ToList();

So i can fill my GridControl DataSource.

gcArtico01S00_LifoFifoGestione.DataSource = LstCurrClassePerGestioneDettaglio;

0

There are 0 best solutions below