Note that there are some explanatory texts on larger screens.

plurals
  1. POEntitySet vs Table query performance in LINQ2SQL
    text
    copied!<p>In a LINQ to SQL class, why are the properties that are created from the foreign keys <code>EntitySet</code> objects, which implement <code>IEnumerable</code>, where as the objects on the <code>DataContext</code> are <code>Table</code> objects which implement <code>IQueryable</code>?</p> <p><strong>EDIT:</strong> To clarify, here is an example that illustrates what I'm trying to understand. This example:</p> <pre><code>ctx.Matches.Where(x =&gt; x.MatchID == 1).Single() .MatchPlayers.Max(x =&gt; x.Score); </code></pre> <p>hits the database twice where as:</p> <pre><code>ctx.MatchPlayers.Where(x =&gt; x.MatchID == 1) .Max(x =&gt; x.Score); </code></pre> <p>only runs 1 query. Here are the traces:</p> <pre><code>exec sp_executesql N'SELECT [t0].[MatchID], [t0].[Date] FROM [dbo].[Matches] AS [t0] WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1 go exec sp_executesql N'SELECT [t0].[MatchID], [t0].[PlayerID], [t0].[Score] FROM [dbo].[MatchPlayers] AS [t0] WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1 go </code></pre> <p>and</p> <pre><code>exec sp_executesql N'SELECT MAX([t0].[Score]) AS [value] FROM [dbo].[MatchPlayers] AS [t0] WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1 go </code></pre> <p>which also shows that, even worse, the max is done at the C# level rather than in the database.</p> <p>I know that the reason this happens is the difference between <code>IQueryable</code>s and <code>IEnumerable</code>s, so why doesn't the <code>MatchPlayers</code> object in the first example implement the <code>IQueryable</code> interface to get the same benefits as the latter example.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload