views:

41

answers:

1

Hello

Wondered if there is a way to provide table name for linq query at runtime. I am interested in simple quert like "select * from @someTableName".

I've searched a lot for the answer but couldnt find any help on the net. There was a post on stackOverflow --> link

Dave Russel suggested to do:

"var p = ctx.GetType.GetProperty(oName).getValue(ctx,null)"

But apart from getting property with Reflection I dunno how to work out the rest in order to be able execute querries against that property like:

from x in p select x;

I'd be greateful if u could point me towards some solution how to solve this issue.

A: 

Try the ExecuteStoreQuery method. You execute a SQL query and obtain ObjectResult. Please note that this method is available only in Entity Framework v4.

Devart
It's not at all clear from the question if he *really* wants table names or if he actually wants entity names. This method will use table names. For entity names, use ESQL or `System.Linq.Dynamic`.
Craig Stuntz
I have various tables with various columns and they are all mapped to Entity Model. So at runtime I pick some table name and retrieve all records from that particular table. So all I have at runtime is table name but I dunno how to use it in order to retrive data(need ordered IQueryable for jqGrid). Doesnt ExecuteStoreQuery has to be parametrized before compilation or I am doing something wrong here?
gixx
Oh and entity names are same as table names in data store.
gixx
Craig is right about Entity SQL. In case you have entity name corresponding to the table name and their structure coincides as well, then the best solution seems to be the CreateQuery method. http://msdn.microsoft.com/en-us/library/bb339670.aspx - here is an example, you can just cast ObjectQuery<T> to IQueryable<T>.
Devart