In a LINQ to SQL class, why are the properties that are created from the foreign keys EntitySet objects, which implement IEnumerable, where as the objects on the DataContext are Table objects which implement IQueryable?

EDIT: To clarify, here is an example that illustrates what I'm trying to understand. This example:

which also shows that, even worse, the max is done at the C# level rather than in the database.

I know that the reason this happens is the difference between IQueryables and IEnumerables, so why doesn't the MatchPlayers object in the first example implement the IQueryable interface to get the same benefits as the latter example.

3 Answers
3

Tables are effectively a conceptual matter - they really exist on the server, so you need to query to get entries. The foreign key entries are the ones actually fetched by another query, so at that point they're locally available. That's a fairly woolly description, but hopefully it gets over the general concept.

This was addressed on the MSDN forums. The gist of the reasoning is that it's very difficult to track added and removed objects while making queries against the database. Instead, the EntitySet is something of a local copy of the related objects that you can manipulate. Unfortunately, as you noticed, this has the side effect of devolving expressions into LINQ to Objects calls instead of the better performing LINQ to SQL.