Select execution time of columns in LINQ LIST <>

I have List<Employee> listEmployees

I want to select some dynamic columns that are not known at compile time, for example:

 var result = from l listEmployees
              select "" 

      

I tried listEmployees.select("Name");

But it throws an error:

'System.Linq.Enumerable.Select (System.Collections.Generic.IEnumerable, System.Func)' cannot be deprecated. Try to specify type arguments explicitly.

How do I select only those columns that are defined at runtime?

+3


source share


3 answers


You are looking for Dynamic LINQ .



+7


source


You can use dynamic linq, this is an open source project I used for really dynamic linq queries:

Scott's blog article



Hope it helps.

+1


source


Here is a really terrible approach that works

var result = from l in listEmployees
             select l.GetType().GetProperty(propertyName).GetValue(l, null);

      

Where propertyName is any string you pass at runtime.

Of course, you probably want to extract the bit after being selected as a method and add some validation.

+1


source







All Articles