Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Neither of these answers so far is quite what I'm looking for. To give an example of what I'm aiming at (I don't regard this as a complete answer either), I took the above code and created a couple of extension methods:</p> <pre><code>static public IQueryable&lt;Activity&gt; AddCondition( this IQueryable&lt;Activity&gt; results, DropDownList ddl, Expression&lt;Func&lt;Activity, bool&gt;&gt; containsCondition) { if (!string.IsNullOrEmpty(ddl.SelectedItem.Text)) results = results.Where(containsCondition); return results; } static public IQueryable&lt;Activity&gt; AddCondition( this IQueryable&lt;Activity&gt; results, CheckBox chk, Expression&lt;Func&lt;Activity, bool&gt;&gt; emptyCondition) { if (chk.Checked) results = results.Where(emptyCondition); return results; } </code></pre> <p>This allowed me to refactor the code above into this:</p> <pre><code>results = results.AddCondition(ddlFileName, x =&gt; x.FileName.Contains(ddlFileName.SelectedValue)); results = results.AddCondition(chkFileName, x =&gt; x.FileName == null || x.FileName.Equals(string.Empty)); results = results.AddCondition(ddlIPAddress, x =&gt; x.IpAddress.Contains(ddlIPAddress.SelectedValue)); results = results.AddCondition(chkIPAddress, x =&gt; x.IpAddress == null || x.IpAddress.Equals(string.Empty)); </code></pre> <p>This isn't <em>quite</em> as ugly, but it's still longer than I'd prefer. The pairs of lambda expressions in each set are obviously very similar, but I can't figure out a way to condense them further...at least not without resorting to dynamic LINQ, which makes me sacrifice type safety.</p> <p>Any other ideas?</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