Returning IQueryable vs. ObjectQuery when using LINQ to Entities

3.7k Views Asked by At

I have been reading when using LINQ to entites a query is of type IQueryable before it is processed but when the query has been processed, it's no longer an IQueryable, but an ObjectQuery.

In that case, is it correct to write methods from my layer (repository layer) to return IQueryable?

Do I need to cast?

Why would I want to return an ObjectQuery?

I'm coming from a LINQ to SQL background where things were always IQueryable but EF seems to have changed this.

Any help really appreciated.

1

There are 1 best solutions below

1
On BEST ANSWER

My repositories always returns IQueryable. The reason for this is that IQueryable is not dependent on EF whereas ObjectQuery is. So if I want my other layers to be persistance ignorant I don't want to introduce dependency on ObjectQuery.

ObjectQuery is implementation of IQueryable with several additional features. First feature you will quickly discover is Include function which is need for eager loading of navigation properties (related entities). Second feature is EQL - entity query language. EQL is second way how you can query your conceptual model. It has similar syntax as SQL. You can write simple query as string, pass it to ObjectQuery constructor and execute query or use it in Linq-To-Entities.