Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you call linq fields dynamically

Tags:

c#

linq

if I'm passing in a string column name, how do I call a distinct list of items by property name?

private myEntities db = new myEntities();

...
//function passes in string
var vals = db.myEntityClass
                .Select(v => v.????).Distinct() //I want this to be selected dynamically
like image 663
Chris Hayes Avatar asked Jun 13 '26 00:06

Chris Hayes


1 Answers

If you are using .NET 4.0, here's a post by David Fowler that makes use of the new dynamic type feature to create a DynamicQueryable and DynamicExpressionBuilder which allows you to reference entity properties dynamically.

Or.. if you rather get straight to it, he's also created a library http://bitbucket.org/dfowler/dynamiclinq the encapsulates the functionality. It's also on NuGet :)

like image 76
cecilphillip Avatar answered Jun 16 '26 12:06

cecilphillip