问题描述:

I am new to EF and LINQ, hoping to get some answers here.

I am trying to search from list with where condition that is according to the selecteditem from a combobox.

The combobox has 15 items(all bit datatypes) but let me just narrow it down to 2 for examples sake. items are( pro bono, civil)

Now I have a list called listOfAllNeutrals (object name is Neutral with properties such as pro bono (bit) and civil(bit), and I want to filter it using the where condition according to the selected item.

so if selected item = pro bono, the linq would look like this

var result = from n in listOfAllNeutrals

where n.probono==true

select n;

but my prob is how do I tell that the n.property should be according to the selecteditem?

like this:

var result = from n in listOfAllNeutrals

where getpropertyName==true

select n;

is there a simpler way, I don't want to use If conditions if possible.

网友答案:

You need to use if's or switch inside the getpropertyName(Neutral objNeutral) function to Map the selected item to the property you want to evaluate and evaluate it. You need to map object => property in some way.

网友答案:

Try

var result = from n in listOfAllNeutrals 
             where (selectedItem == proBono && n.probono == true)
             || (selectedItem == civil && n.civil == true)
             select n;
相关阅读:
Top