mercredi 27 avril 2016

How can I filter a list with dynamic properties selection in c#?

I need to filter a collection with dynamic properties selection.

Example:

public class NotificationListModel : Observable
{ 
    private string _QMTXT;

    public string QMTXT
    {
        get { return _QMTXT; }
        set { _QMTXT = value; RaisePropertyChanged("QMTXT"); }
    }
    private string _PRIOK;

    public string PRIOK
    {
        get { return _PRIOK; }
        set { _PRIOK = value; RaisePropertyChanged("PRIOK"); }
    }
    private string _ARBPL;

    public string ARBPL
    {
        get { return _ARBPL; }
        set { _ARBPL = value; RaisePropertyChanged("ARBPL"); }
    }
    private string _id;

    public string id
    {
        get { return _id; }
        set { _id = value; RaisePropertyChanged("id"); }
    }

}

And I have a collection NotificationCollection, is having couple of records, so I need to filter this collection with different properties and those are not fixed like below,

Example 1:

var Result =  NotificationCollection.Where(w =>(w.QMTXT=="1" || w.QMTXT=="2") && w.PRIOK == "1").ToList();

Example2:

 var Result =  NotificationCollection.Where(w =>w.id=="1" && w.PRIOK == "1").ToList();

Here, while filtering the list properties will be dynamic it may filter with QMTXT or PRIOK or combination of QMTXT and PRIOK and some other property. How can I achieve it. I did lot of research I came to know we can do this by using reflection but I don't have that much scope on Reflection.

Your help is very appreciable. Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire