Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First - some reasoning for the Take bug.</p> <p>If you <strong>just Take</strong>, the query translator just uses top. Top10 will not give the right answer if cardinality is broken by joining in a child collection. So the query translator doesn't join in the child collection (instead it requeries for the children).</p> <p>If you <strong>Skip and Take</strong>, then the query translator kicks in with some RowNumber logic over the parent rows... these rownumbers let it take 10 parents, even if that's really 50 records due to each parent having 5 children.</p> <p>If you <strong>Skip(0) and Take</strong>, Skip is removed as a non-operation by the translator - it's just like you never said Skip.</p> <p>This is going to be a hard conceptual leap to from where you are (calling Skip and Take) to a "simple workaround". What we need to do - is force the translation to occur at a point where the translator can't remove Skip(0) as a non-operation. We need to call Skip, and supply the skipped number at a later point.</p> <pre><code>DataClasses1DataContext myDC = new DataClasses1DataContext(); //setting up log so we can see what's going on myDC.Log = Console.Out; //hierarchical query - not important var query = myDC.Options.Select(option =&gt; new{ ID = option.ParentID, Others = myDC.Options.Select(option2 =&gt; new{ ID = option2.ParentID }) }); //request translation of the query! Important! var compQuery = System.Data.Linq.CompiledQuery .Compile&lt;DataClasses1DataContext, int, int, System.Collections.IEnumerable&gt; ( (dc, skip, take) =&gt; query.Skip(skip).Take(take) ); //now run the query and specify that 0 rows are to be skipped. compQuery.Invoke(myDC, 0, 10); </code></pre> <p>This produces the following query:</p> <pre><code>SELECT [t1].[ParentID], [t2].[ParentID] AS [ParentID2], ( SELECT COUNT(*) FROM [dbo].[Option] AS [t3] ) AS [value] FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID]) AS [ROW_NUMBER], [t0].[ParentID] FROM [dbo].[Option] AS [t0] ) AS [t1] LEFT OUTER JOIN [dbo].[Option] AS [t2] ON 1=1 WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p1 + @p2 ORDER BY [t1].[ROW_NUMBER], [t2].[ID] -- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [0] -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [0] -- @p2: Input Int (Size = 0; Prec = 0; Scale = 0) [10] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1 </code></pre> <p>And here's where we win!</p> <pre><code>WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p1 + @p2 </code></pre>
    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