Lets say I have a method like this:
IQueryable<MyFlatObject> GetMyFlatObjects()
{
using (var context = new MyEntities())
{
return context.MyEntities.Select(x => new MyFlatObject()
{
Property1 = x.PropertyA,
Property2 = x.PropertyB,
Property3 = x.PropertyC,
});
}
}
Now if I call:
MyService.GetMyFlatObjects().Where(x => x.Property1 == "test");
Sanity check. This filter will not propagate to my database store (like if I had just queried my entities), but instead I will get all results back and be using LINQ-to-objects to filter. Right?
I think, it’s not right. First, it doesn’t query anything because you are only extending an
IQueryable<T>to a newIQueryable<T>. If you callToList()or anything else that causes the query to execute you’ll get an exception because the context has already been disposed at the end of theusingblock. If you don’t dispose the context theWherefilter will be translated to SQL and executed in the database. I believe it will behave the same way as if you would apply theWheretoPropertyAbefore theSelect.