views:

264

answers:

1

I have the following LINQ to Entities query...

var results = from c in context.Contacts
              select c;

which works fine in returning a collection of contacts. But I have seen sample code that does this instead...

ObjectResult<Contact> results = (from c in context.Contacts
                                 select c).Execute();

What is the difference? The ObjectResult also has a collection of returned contacts. Is it just syntactic or is there a real fundamental difference?

+3  A: 

ObjectResult<> is simply the type returned by EF when you start enumerating the IQueryable<> (i.e. context.Contacts).

So if you immediately enumerate either of your two queries, semantically it is the same.

The only difference is that in the first example if compose more query operations they will get appended to the query sent to the database when you enumerate, whereas in the second example they will get applied in memory by LINQ to Objects.

Also Execute(..) provides somewhat easier access to MergeOptions (like, should the database copy overwrite copies already in memory or visa versa). You can do this using the MergeOptions property on the ObjectQuery<> too, but that is a little more cumbersome.

Hope this helps

Alex

Alex James