Varaible where clause in linq

countrymeister
countrymeister used Ask the Experts™
on
I have a generic List T, I need to query this list based on which field in this list is selected , so I need to have a variable where clause, how can I achieve this

List<T>, has GroupCode, Product, Country, Region

Currently I am doing

if (field =  GroupCode)
newfilteredList = (from p in filteredList.Where(r => r.GroupCode == FilterValue)

else if field =  product)
newfilteredList = (from p in filteredList.Where(r => r.product == FilterValue)


I want to do something like
newfilteredList = (from p in filteredList.Where(WhereClause)

Where the WhereClause would be some string witht he FilterField and value
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Distinguished Expert 2017
Commented:
Seeming that Linq is a strongly typed extension using strings in where clause in that way is not supported directly. You can use, "Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)", too accomplish this. At this link you will find samples on how to use it and where to download the library.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial