Note that there are some explanatory texts on larger screens.

plurals
  1. POOrderByWithDirection when direction depends on data
    primarykey
    data
    text
    <p>In this SO article: <a href="https://stackoverflow.com/questions/388708/ascending-descending-in-linq-can-one-change-the-order-via-parameter">ascending/descending in LINQ - can one change the order via parameter?</a> a solution was presented for how to conditionally order in ascending or descending order.</p> <p>what the article doesn't cover is, what if I want to make the decision based on the data in the query (which is, actually, what is most likely)?</p> <p>I cannot seem to do:</p> <pre><code>source.OrderByWithDirection(o =&gt; o.MyColumnToOrderBy, o =&gt; o.SortingDirection) </code></pre> <p>would I then need to rewrite the methods? how?</p> <p><strong>- update -</strong></p> <p>by way of rationale, here is what I'm looking to accomplish:</p> <pre><code>Strike Type Price 98 Ask 101 98 Ask 100 98 Ask 99 98 Bid 95 98 Bid 96 98 Bid 97 </code></pre> <p>as you can see, asks are sorted down but bids are sorted up such that the rows with the greatest difference are next to each other. so what I'd like to say is something like:</p> <pre><code>source.OrderBy(o =&gt; new { o.Strike, o.Type }) .ThenByWithDirection(o =&gt; o.Price, o =&gt; o.Type == "Ask" ? __down__ : __up__) </code></pre> <p><strong>- update II -</strong></p> <p>a clumsy way to do this would be to issue two separate queries like this:</p> <pre><code>source .Where(o =&gt; o.Type == "Ask") .OrderBy(o =&gt; new { o.Strike, o.Type }) .ThenBy(o =&gt; o.Price) source .Where(o =&gt; o.Type == "Bid") .OrderBy(o =&gt; new { o.Strike, o.Type }) .ThenByDescending(o =&gt; o.Price) </code></pre> <p>and concatenate them</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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