Using SqlQuery to Get IQueryable

Is there something that can return IQueryable for dynamic SQL query in Entity Framework 6?

This is what I am using now, but it pulls all the records (as expected).

 DbContext.Database.SqlQuery<T>("SELECT * FROM dbo.SomeDynamicView") 

The problem is that SqlQuery returns a DbRawSqlQuery which is IEnumerable .

dbo.SomeDynamicView is a database view created at runtime.

+17
entity-framework entity-framework-6
01 Oct '14 at 14:19
source share
2 answers

No, you cannot get IQueryable from SqlQuery * because what IQueryable does builds the SQL string dynamically based on what it selects and where you filter it. Since you provide a string in SqlQuery , the Entity Framework cannot generate this dynamic string.

Your parameters either dynamically build a line in which your I should go to SqlQuery , and use it as IEnumerable instead of IQueryable or use the DbSet in your DbContext and do a more β€œnormal” way of providing an object structure for you.




* You can technically by calling AsQueryable () as a result, but it’s just IEnumerable pretending to be IQueryable, it does not give you any advantages of using the β€œReal” IQueryable, for example, only to get the necessary lines from the server.

+18
Oct 01
source share

Yes you can do the following:

 DbContext.Database.SqlQuery("SELECT * FROM dbo.SomeDynamicView").AsQueryable<T>() 
-9
Feb 22 '15 at 22:37
source share



All Articles