Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my attempt using mostly query syntax and using two separate queries:</p> <pre><code>var stids = from st in this.Context.StockTakes orderby st.StockTakeId descending select st.StockTakeId; var topFacts = from stid in stids.Take(topCount) join stf in this.Context.StockTakeFacts on stid equals stf.StockTakeId where stf.FactKindId == (int)kind select stf; return topFacts.ToList&lt;IStockTakeFact&gt;(); </code></pre> <p>As others suggested, what you were looking for is a join. Because the join extension has so many parameters they can be a bit confusing - so I prefer query syntax when doing joins - the compiler gives errors if you get the order wrong, for instance. Join is by far preferable to a filter not only because it spells out how the data is joined together, but also for performance reasons because it uses indexes when used in a database and hashes when used in linq to objects.</p> <p>You should note that I call <code>Take</code> in the second query to limit to the <code>topCount</code> stids used in the second query. Instead of having two queries, I could have used an <code>into</code> (i.e., query continuation) on the select line of the <code>stids</code> query to combine the two queries, but that would have created a mess for limiting it to <code>topCount</code> items. Another option would have been to put the <code>stids</code> query in parentheses and invoked <code>Take</code> on it. Instead, separating it out into two queries seemed the cleanest to me.</p> <p>I ordinarily avoid specifying generic types whenever I think the compiler can infer the type; however, <code>IStockTakeFact</code> is almost certainly an interface and whatever concrete type implements it is likely contained by <code>this.Context.StockTakeFacts;</code> which creates the need to specify the generic type on the <code>ToList</code> call. Ordinarily I omit the generic type parameter to my <code>ToList</code> calls - that seems to be an element of my personal tastes, yours may differ. If <code>this.Context.StockTakeFacts</code> is already a <code>List&lt;IStockTakeFact&gt;</code> you could safely omit the generic type on the <code>ToList</code> call.</p>
    singulars
    1. This table or related slice is empty.
    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. 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