0

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. .

1 Answer 1

1

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;
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.