?
Solved

Varaible where clause in linq

Posted on 2014-02-26
1
Medium Priority
?
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

764 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