Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a simple explanation:</p> <pre><code>IEnumerable&lt;User&gt; usersEnumerable = db.UserSet; IQueryable&lt;User&gt; usersQueryable = db.UserSet; var users = /* one of usersEnumerable or usersQueryable */; var age32StartsWithG = users.Where(user =&gt; user.Age == 32) .Where(user =&gt; user.Name.StartsWith("G"); </code></pre> <p>If you use <code>usersEnumerable</code>, when you start enumerating over it, the two <code>Where</code>s will be run in sequence; first the <code>ObjectSet</code> will fetch all objects and the objects will be filtered down to those of age 32, and then these will be filtered down to those whose name starts with G.</p> <p>If you use <code>usersQueryable</code>, the two <code>Where</code>s will return new objects which will accumulate the selection criteria, and when you start enumerating over it, it will translate all of the criteria to a query. This makes a noticeable difference.</p> <p>Normally, you don't need to worry, since you'll either say <code>var users</code> or <code>ObjectSet users</code> when you declare your variable, which means that C# will know that you are interested in invoking the most specific method that's available on <code>ObjectSet</code>, and the <code>IQueryable</code> query operator methods (<code>Where</code>, <code>Select</code>, ...) are more specific than the <code>IEnumerable</code> methods. However, if you pass around objects to methods that take <code>IEnumerable</code> parameters, they might end up invoking the wrong methods.</p> <p>You can also use the way this works to your advantage by using the <code>AsEnumerable()</code> and <code>AsQueryable()</code> methods to start using the other approach. For example, <code>var groupedPeople = users.Where(user =&gt; user.Age &gt; 15).AsEnumerable().GroupBy(user =&gt; user.Age);</code> will pull down the right users with a database query and then group the objects locally.</p> <p>As other have said, it's worth repeating that nothing happens until you start enumerating the sequences (with <code>foreach</code>). You should now understand why it couldn't be any other way: if all results were retrieved at once, you couldn't build up queries to be translated into a more efficient query (like an SQL query).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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