Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The difficulty with using Futures with LINQ is that operations like Count execute immediately.</p> <p>As @vandalo found out, <code>Count()</code> after <code>ToFuture()</code> actually runs the Count in memory, which is bad.</p> <p>The only way to get the count in a future LINQ query is to use <code>GroupBy</code> in an invariant field. A good choice would be something that is already part of your filters (like an "IsActive" property)</p> <p>Here's an example assuming you have such a property in Payment:</p> <pre><code>//Create base query. Filters should be specified here. var query = session.Query&lt;Payment&gt;().Where(x =&gt; x.IsActive == 1); //Create a sorted, paged, future query, //that will execute together with other statements var futureResults = query.OrderByDescending(payment =&gt; payment.Created) .Skip((page - 1) * pageSize) .Take(pageSize) .ToFuture(); //Create a Count future query based on the original one. //The paged query will be sent to the server in the same roundtrip. var futureCount = query.GroupBy(x =&gt; x.IsActive) .Select(x =&gt; x.Count()) .ToFutureValue(); //Get the results. var results = futureResults.ToArray(); var count = futureCount.Value; </code></pre> <p>Of course, the alternative is doing two roundtrips, which is not that bad anyway. You can still reuse the original IQueryable, which is useful when you want to do paging in a higher-level layer:</p> <pre><code>//Create base query. Filters should be specified here. var query = session.Query&lt;Payment&gt;(); //Create a sorted, paged query, var pagedQuery = query.OrderByDescending(payment =&gt; payment.Created) .Skip((page - 1) * pageSize) .Take(pageSize); //Get the count from the original query var count = query.Count(); //Get the results. var results = pagedQuery.ToArray(); </code></pre> <p><strong>Update</strong> (2011-02-22): I wrote a <a href="http://sessionfactory.blogspot.com/2011/02/getting-row-count-with-future-linq.html">blog post</a> about this issue and a much better solution.</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. 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.
 

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