Solved

Varaible where clause in linq

Posted on 2014-02-26
1
374 Views
Last Modified: 2016-02-10
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
0
Comment
Question by:countrymeister
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39889986
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.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now