Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities - Dynamic select for specify columns

I can create a select query that retrieves only the specific columns (static in code) for LINQ to entities

from Example in new Enities.Table 
select new { Example.Column1. Example.Column2, ... }

But I can´t figure out how to select Column1, Column2 dynamic (e.g. from string[]). Thanks

like image 229
Pavel Jedlicka Avatar asked Aug 05 '11 09:08

Pavel Jedlicka


People also ask

How do I select specific columns in Entity Framework?

We can do that simply by using the “new” operator and selecting the properties from the object that we need. In this case, we only want to retrieve the Id and Title columns. There.

Can you use Linq on dynamic?

It's possible to build up dynamic LINQ queries or queries with several conditional criteria. In fact there are several options for doing this, including the use of expression trees.

What is Dynamic Linq?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.


1 Answers

Standard LINQ doesn't support that - you must download library called Dynamic LINQ or build expression tree manually. Dynamic LINQ will allow you to call queries like:

var query = Entities.Table.Select("new(Column1,Column2)");

But by using dynamic approach you will lose the main reason for using LINQ - compile time checking. You can in the same way use Entity SQL instead of LINQ and build your queries from strings.

like image 163
Ladislav Mrnka Avatar answered Nov 02 '22 10:11

Ladislav Mrnka