tags:

views:

53

answers:

4

From a custom SQL query I'd like to get an IQueryable (I have my reasons):

Something like:

IQueryable<Client> query = Foo<Client>("SELECT * FROM Clients WHERE ...");

query.Where(e => e.Active==true).Skip(10).Take(10); //etc.

Is there any way to implement Foo?

I found ExecuteQuery<T>(...).AsQueryable(), but that doesn't work as it loads all records.

+1  A: 

You could implement this but you'd have to parse the SQL text and look up the mapping to get the correct types (e.g., from Clients to Client). There's no built-in facility for automatically generating an IQueryable from SQL.

Mark Cidade
+1  A: 

From my point of view, the best way would be to implement your own wrapper inheriting IQueryable for such cases. In GetEnumerator() you can implement a row-by-row results reading or simply return ExecuteQuery(...).GetEnumerator() from your own GetEnumerator() method.

ILya
+2  A: 

Can you put this part of your query in a view?

SELECT * FROM Clients...

Then you can use LINQ Where and Skip/Take.

var results = db.MyViews.Where(e => e.Active == true).Skip(10).Take(10);
RedFilter
No, to clarify, the SQL is user submitted. (I'm working on a admin tool)
Niels Bosma
In that case I would wrap the query in another query to handle paging, you are in the land of dynamic SQL at this point I think. What database are you using?
RedFilter