Snj
Snj

Reputation: 963

create lambda expressions

I'm trying to filter a object list (List) bounded to a grid. Now I want to filter this list according to the user required. I have combo box to select the field to filter and text box to enter the value. From there user can select ItemCode or Cost or any property relevant to Item class. Then how can I create the lambda expression according to the selected field and entered value. .

Upvotes: 0

Views: 119

Answers (1)

Bala R
Bala R

Reputation: 108947

One flexible but not very simple option would be to use Dynamic LINQ. You can construct the query based of the user selection and even use multiple properties with AND and OR operations and comparison operators, etc and use it to filter results. Here's ScottGu's post on dynamic LINQ.

The other straightforward option would be do have a giant switch case for each property in the class.

...
case "ItemCode":
       results = records.Where(i => i.ItemCode == criteriaValue);
       break;
case "Cost":
       results = recotds.Where(i => i.Cost == Convert.ToDouble(criteriaValue));
       break;
...

Upvotes: 1

Related Questions