ASP.NET - Dynamic Select Statement in linq to entity

Asked By ananda shankar on 15-Feb-12 01:45 AM

Hi EveryOne,  I need to convert a SP in to a LinQ to entity model. And in SP i m having a dynamic query which selects the column dynamicall like Select + @Colname + From ..... and so in Linq To Entity how can i do this... (ie Dynamic Select statement) please help me... Thanks in advance.....

Somesh Yadav replied to ananda shankar on 15-Feb-12 02:03 AM

Standard LINQ doesn't support that - you must download library called http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx 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 http://msdn.microsoft.com/en-us/library/bb387118.aspx instead of LINQ and build your queries from strings.