Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq-to-SQL EntitySet Is Not IQueryable -- Any Workarounds?

When you query an EntitySet property on a model object in Linq-to-SQL, it returns all rows from the entityset and does any further querying client-side.

This is confirmed in a few places online and I've observed the behavior myself. The EntitySet does not implement IQueryable.

What I've had to do is convert code like:

var myChild = ... ;
// Where clause performed client-side.
var query = myChild.Parents().Where(...) ;  

to:

var myChild = ... ;
// Where clause performed in DB and only minimal set of rows returned.
var query = MyDataContext.Parents().Where(p => p.Child() == myChild) ;  

Does anyone know a better solution?

A secondary question: is this fixed in the Entity Framework?

like image 619
Pete Avatar asked Mar 02 '11 20:03

Pete


1 Answers

An EntitySet is just a collection of entities. It implements IEnumerable, not IQueryable. The Active Record pattern specifies that entities be directly responsible for their own persistence. OR mapper entities don't have any direct knowledge of the persistence layer. OR Mappers place this responsibility, along with Unit Of Work, and Identity Map responsibilities into the Data Context. So if you need to query the data source, you gotta use the context (or a Table object). To change this would bend the patterns in use.

like image 141
mikesigs Avatar answered Nov 15 '22 17:11

mikesigs