Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.Linq.Expressions.Expression for .OrderBy function
    text
    copied!<p>I have a line section of my code that is repetitive.</p> <p>You will see the lambda expression below repeat 4 times</p> <pre><code>x =&gt; x.GetValue&lt;string&gt;("City")).ThenByDescending(x =&gt; HelperFunctions.strToTZDateTime(x.GetValue&lt;string&gt;("EventStartStr"), x.GetValue&lt;string[]&gt;("EventTimeZone")[0].ToString()) </code></pre> <p>The code example :</p> <pre><code>//SORT DATA switch (SortDetails) { case ("Date"): default: if (SortOrder == "ASC") { _allEvents = _allEvents.OrderBy(x =&gt; HelperFunctions.strToTZDateTime(x.GetValue&lt;string&gt;("EventStartStr"), x.GetValue&lt;string[]&gt;("EventTimeZone")[0].ToString())); } else { _allEvents = _allEvents.OrderByDescending(x =&gt; HelperFunctions.strToTZDateTime(x.GetValue&lt;string&gt;("EventStartStr"), x.GetValue&lt;string[]&gt;("EventTimeZone")[0].ToString())); } break; case ("Location"): if (SortOrder == "ASC") { _allEvents = _allEvents.OrderBy(x =&gt; x.GetValue&lt;string&gt;("Country")).ThenBy(x =&gt; x.GetValue&lt;string&gt;("ProvinceState")).ThenBy(x =&gt; x.GetValue&lt;string&gt;("City")).ThenBy(x =&gt; HelperFunctions.strToTZDateTime(x.GetValue&lt;string&gt;("EventStartStr"), x.GetValue&lt;string[]&gt;("EventTimeZone")[0].ToString())); } else { _allEvents = _allEvents.OrderByDescending(x =&gt; x.GetValue&lt;string&gt;("Country")).ThenByDescending(x =&gt; x.GetValue&lt;string&gt;("ProvinceState")).ThenByDescending(x =&gt; x.GetValue&lt;string&gt;("City")).ThenByDescending(x =&gt; HelperFunctions.strToTZDateTime(x.GetValue&lt;string&gt;("EventStartStr"), x.GetValue&lt;string[]&gt;("EventTimeZone")[0].ToString())); } break; } </code></pre> <p>I would like to take the first line and make it into something I can just plop into the OrderBy's.</p> <p>Something like : </p> <pre><code>System.Linq.Expressions.Expression&lt;Func&lt;DynamicContent, DateTime&gt;&gt; sortLambda = x =&gt; HelperFunctions.strToTZDateTime(x.GetValue&lt;string&gt;("EventStartStr"), x.GetValue&lt;string[]&gt;("EventTimeZone")[0].ToString()); </code></pre> <p>into :</p> <pre><code>allEvents = _allEvents.OrderBy(sortLambda); </code></pre> <p>Unfortunately that didn't seem to work well.</p>
 

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