Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are calling <code>GetCommentsByPostId</code> within what is ultimately an expression tree. That tree, when composed in <code>BlogService.GetPublicPosts</code>, is converted to SQL.</p> <p>During that conversion, it is just a method call, nothing more. Linq to Sql understands certain method calls, and yours is not one of them. Hence the error.</p> <p>On the surface, this seems like it should work. You write reusable queries and compose them from other queries. However, what you are actually saying is: "during the processing of each row on the database server, call this method", which it obviously can't do. The fact that it takes an <code>IQueryable&lt;T&gt;</code> and returns an <code>IQueryable&lt;T&gt;</code> does not make it special.</p> <p>Think about it this way: you are passing <code>postId</code> to <code>GetCategoriesByPostId</code>. You can't call that method until you have a <code>postId</code>, and you don't have one of those until you are on the server in the query.</p> <p>You would probably need to define common <code>Expression&lt;&gt;</code> instances for the sub-queries and use those in the composition. I haven't thought about what this would look like but it's certainly doable.</p> <p><strong>Edit:</strong></p> <p>If you replace</p> <pre><code>let categories = GetCategoriesByPostId(p.PostId) let comments = GetCommentsByPostId(p.PostId) ... Categories = new LazyList&lt;Category&gt;(categories), Comments = new LazyList&lt;Comment&gt;(comments), </code></pre> <p>with</p> <pre><code>Categories = new LazyList&lt;Category&gt;(GetCategoriesByPostId(p.PostId)), Comments = new LazyList&lt;Comment&gt;(GetCommentsByPostId(p.PostId)), </code></pre> <p>the query will no longer throw an exception.</p> <p>This is because <code>let</code> declares range variables, which are in scope for each row. They <strong>must</strong> be calculated on the server.</p> <p>Projections, however, allow you to put arbitrary code in assignments, which is then executed while building results on the client. This means both methods will be called, each of which will issue its own 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. 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