Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning IQueryable vs. ObjectQuery when using LINQ to Entities

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.

like image 1000
mark smith Avatar asked Dec 07 '25 09:12

mark smith


1 Answers

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.

like image 142
Ladislav Mrnka Avatar answered Dec 09 '25 02:12

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!