Sean Thoman
Sean Thoman

Reputation: 7489

Overloading .Where extension with IQueryable<T> implementation

My question is how / when it makes sense to overload (if thats possible?) the Where() extension method of IQueryable when you are making your own IQueryable implementation?

For example in Entity Framework its my understanding that a Where() call made against an ObjectSet will change the actual SQL thats being passed to the database. Alternatively if you cast to IEnumerable() first the filtering is done with LINQ-To-Objects rather than LINQ-To-Entities.

For instance:

new MyDBEntities().MyDbTable.Where(x => x.SomeProperty == "SomeValue"); 
  // this is linq-to-entities and changes the database-level SQL

Versus:

new MyDBEntities().MyDbTable.AsEnumerable().Where(x => x.SomeProperty == "SomeValue"); 
  // this is linq-to-objects and filters the IEnumerable 

How do you deal with this when implementing your own IQueryable and IQueryable already has pre-defined extension methods such as Where()? Specifically I want make my own very simple ORM that uses native SQL and SqlDataReader under the hood, and want to have a .Where() method that changes the native SQL before passing it to the database.

Should I even use IQueryable or just create my own class entirely? I want to be able to use lambda syntax and have my SQL command altered based on the lambda function(s) used for filtration.

Upvotes: 0

Views: 491

Answers (1)

svick
svick

Reputation: 244948

You could create your own type analogous to IQueryable<T>, but that's probably not a good idea. You should probably write your own implementation of the interface. There is a nice series of articles on how to do it, but be prepared, doing so is not one of the easier tasks.

Upvotes: 1

Related Questions