Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your <strong>first query</strong>, LINQ to SQL knows it <strong><em>only</em></strong> needs to return a scalar value <code>GroupID</code> (or a <code>GroupID</code> array). So that's what it does: LINQ to SQL queries <em>only</em> that column in the database and returns only those value(s).</p> <pre><code>From g In Db.Context.Current.Groups Select g.GroupID </code></pre> <p>In your <strong>second query</strong>, LINQ to SQL knows it needs to return a scalar (same as the first query), <strong>plus the value returned by a property/method call on an actual entity instance</strong>, a Group object. So, to be able to call <code>MemberCount()</code> there must first be a Group object to call it on, right? So LINQ to SQL must do a full entity fetch, and that's why it's querying all the columns.</p> <pre><code>From g In Db.Context.Current.Groups Select g.GroupID, g.MemberCount 'property/method call on an entity </code></pre> <p>[Just theorizing here...] You might say, But <code>MemberCount()</code> doesn't need a full entity fetch, it doesn't need all that data - all it needs is <code>GroupID</code>. And I would reply, How does LINQ to SQL know that? How can LINQ to SQL know that your method <code>MemberCount()</code> <em>doesn't</em> rely on the data of other db-column-based properties? It can't know that, so it must return the full object. [End theorizing.]</p> <p><strong>Suggestion:</strong> Join <code>ContactsGroupsXtabs</code> in your second query, group on <code>GroupID</code> and do a <code>Count()</code> on the group to get your value. (I can see by your other SO questions and answers that you know how to do all that so I'll skip the 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