How can I dynamically create a filter on a generic list

I have a generic List, and I need to create a filter on this list

Not sure if this is possible, I need to replace the row.Field with some dynamically created string

 var list = from rows in listData.AsEnumerable().Where(row => row.Field = sCol )
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Will the sCol variable vary from element to element in the List?
Daniel Van Der WerkenIndependent ConsultantCommented:
I don't know of any way to do this off hand. It is possible in scripting language like JavaScript to do something like this, but not in C#. Therefore, I can see why you might think it's possible.

The best solution I can offer is a switch statement:

   case: ThingyOne:
      list = from rows in listData.AsEnumerable().Where(row => row.Field = sCol);
   case: ThingyTwo:
       list = from rows in listData.AsEnumerable().Where(row => row.SomethingElse= sCol);

Open in new window

Now, that's kind of old-school, though. You might be able to implement something that uses Functional Programming. I'm still learning that, myself, though
käµfm³d 👽Commented:
I have a generic List
If you do, then there's no need to call the AsEnumerable method. A List implements IEnumerable, so the LINQ methods are readily available without the need for AsEnumerable.

Also, a generic list of what? I suppose it's not strings. What does your class look like? In other words, if you have List<T>, show us the definition of "T".
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

countrymeisterAuthor Commented:

The sCol value is a string whose value will not change.

List <T>, T has 10 properties some of which are strings, decimals and bool.


Thanks for the suggestion, but I would have to code for every property, trying to avoid doing that.
Fernando SotoRetiredCommented:
Hi countrymeister;

Then this should just change the property Field to the value of sCol in every instance of the list.

// Some dynamically created strain
string sCol = "Dynamically created string";
listData.ForEach(row => row.Field =  sCol);

// The value of Field has been changed in all instance

Open in new window

countrymeisterAuthor Commented:

I want the row.Field to be one of the fields that the user selects and sCol is the value that needs to be filtered, so if the user selected Field1 then I want to filter Field1 with the value sCol.

If Field2 was selected I want to filter Field2 with the value sCol.

I have been thinking of converting the list to a datatable and then doing dr["somefield"] = sCol

Dan7el's solution was another way of doing this.
Fernando SotoRetiredCommented:
Hi countrymeister;

You can do that with a Linq library which will allow you to create a where claus which is a string and the library will convert it to a strongly type query. Please see the following post to see how to use the library and where it can be downloded.

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.