Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I created 2 extension methods <code>WhereFilter()</code> for <code>IEnumerable</code> and <code>IQueryable</code>. At this way you can use this filter also with e.g. Entity Framework and is the filtering performed on the server.</p> <p>I used a filter based on <em>*</em> (not ?) so i could use the underlaying Linq methods <code>StartsWith()</code>, <code>EndsWith()</code> and <code>Contains()</code>. Supported formats: A*, *A, *A*, A*B </p> <p>Usage:</p> <pre><code>var filtered = list.WhereFilter(i =&gt; i.Name, "a*", "First Name"); </code></pre> <p>Here the basics of the class:</p> <pre><code>/// &lt;summary&gt; /// Extension Methods for Filtering on IQueryable and IEnumerable /// &lt;/summary&gt; internal static class WhereFilterExtensions { /// &lt;summary&gt; /// Filters a sequence of values based on a filter with asterix characters: A*, *A, *A*, A*B /// &lt;/summary&gt; /// &lt;param name="source"&gt;&lt;/param&gt; /// &lt;param name="selector"&gt;Field to use for filtering. (E.g: item =&gt; item.Name)&lt;/param&gt; /// &lt;param name="filter"&gt;Filter: A*, *A, *A*, A*B&lt;/param&gt; /// &lt;param name="fieldName"&gt;Optional description of filter field used in error messages&lt;/param&gt; /// &lt;returns&gt;Filtered source&lt;/returns&gt; public static IEnumerable&lt;T&gt; WhereFilter&lt;T&gt;(this IEnumerable&lt;T&gt; source, Func&lt;T, string&gt; selector, string filter, string fieldName) { if (filter == null) return source; if (selector == null) return source; int astrixCount = filter.Count(c =&gt; c.Equals('*')); if (astrixCount &gt; 2) throw new ApplicationException(string.Format("Invalid filter used{0}. '*' can maximum occur 2 times.", fieldName == null ? "" : " for '" + fieldName + "'")); if (filter.Contains("?")) throw new ApplicationException(string.Format("Invalid filter used{0}. '?' is not supported, only '*' is supported.", fieldName == null ? "" : " for '" + fieldName + "'")); // *XX* if (astrixCount == 2 &amp;&amp; filter.Length &gt; 2 &amp;&amp; filter.StartsWith("*") &amp;&amp; filter.EndsWith("*")) { filter = filter.Replace("*", ""); return source.Where(item =&gt; selector.Invoke(item).Contains(filter)); } // *XX if (astrixCount == 1 &amp;&amp; filter.Length &gt; 1 &amp;&amp; filter.StartsWith("*")) { filter = filter.Replace("*", ""); return source.Where(item =&gt; selector.Invoke(item).EndsWith(filter)); } // XX* if (astrixCount == 1 &amp;&amp; filter.Length &gt; 1 &amp;&amp; filter.EndsWith("*")) { filter = filter.Replace("*", ""); return source.Where(item =&gt; selector.Invoke(item).StartsWith(filter)); } // X*X if (astrixCount == 1 &amp;&amp; filter.Length &gt; 2 &amp;&amp; !filter.StartsWith("*") &amp;&amp; !filter.EndsWith("*")) { string startsWith = filter.Substring(0, filter.IndexOf('*')); string endsWith = filter.Substring(filter.IndexOf('*') + 1); return source.Where(item =&gt; selector.Invoke(item).StartsWith(startsWith) &amp;&amp; selector.Invoke(item).EndsWith(endsWith)); } // XX if (astrixCount == 0 &amp;&amp; filter.Length &gt; 0) { return source.Where(item =&gt; selector.Invoke(item).Equals(filter)); } // * if (astrixCount == 1 &amp;&amp; filter.Length == 1) return source; // Invalid Filter if (astrixCount &gt; 0) throw new ApplicationException(string.Format("Invalid filter used{0}.", fieldName == null ? "" : " for '" + fieldName + "'")); // Empty string: all results return source; } /// &lt;summary&gt; /// Filters a sequence of values based on a filter with asterix characters: A*, *A, *A*, A*B /// &lt;/summary&gt; /// &lt;param name="source"&gt;&lt;/param&gt; /// &lt;param name="selector"&gt;Field to use for filtering. (E.g: item =&gt; item.Name) &lt;/param&gt; /// &lt;param name="filter"&gt;Filter: A*, *A, *A*, A*B&lt;/param&gt; /// &lt;param name="fieldName"&gt;Optional description of filter field used in error messages&lt;/param&gt; /// &lt;returns&gt;Filtered source&lt;/returns&gt; public static IQueryable&lt;T&gt; WhereFilter&lt;T&gt;(this IQueryable&lt;T&gt; source, Expression&lt;Func&lt;T, string&gt;&gt; selector, string filter, string fieldName) { if (filter == null) return source; if (selector == null) return source; int astrixCount = filter.Count(c =&gt; c.Equals('*')); if (astrixCount &gt; 2) throw new ApplicationException(string.Format("Invalid filter used{0}. '*' can maximum occur 2 times.", fieldName==null?"":" for '" + fieldName + "'")); if (filter.Contains("?")) throw new ApplicationException(string.Format("Invalid filter used{0}. '?' is not supported, only '*' is supported.", fieldName == null ? "" : " for '" + fieldName + "'")); // *XX* if (astrixCount == 2 &amp;&amp; filter.Length &gt; 2 &amp;&amp; filter.StartsWith("*") &amp;&amp; filter.EndsWith("*")) { filter = filter.Replace("*", ""); return source.Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Call(selector.Body, "Contains", null, Expression.Constant(filter)), selector.Parameters[0] ) ); } // *XX if (astrixCount == 1 &amp;&amp; filter.Length &gt; 1 &amp;&amp; filter.StartsWith("*")) { filter = filter.Replace("*", ""); return source.Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Call(selector.Body, "EndsWith", null, Expression.Constant(filter)), selector.Parameters[0] ) ); } // XX* if (astrixCount == 1 &amp;&amp; filter.Length &gt; 1 &amp;&amp; filter.EndsWith("*")) { filter = filter.Replace("*", ""); return source.Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Call(selector.Body, "StartsWith", null, Expression.Constant(filter)), selector.Parameters[0] ) ); } // X*X if (astrixCount == 1 &amp;&amp; filter.Length &gt; 2 &amp;&amp; !filter.StartsWith("*") &amp;&amp; !filter.EndsWith("*")) { string startsWith = filter.Substring(0, filter.IndexOf('*')); string endsWith = filter.Substring(filter.IndexOf('*') + 1); return source.Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Call(selector.Body, "StartsWith", null, Expression.Constant(startsWith)), selector.Parameters[0] ) ).Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Call(selector.Body, "EndsWith", null, Expression.Constant(endsWith)), selector.Parameters[0] ) ); } // XX if (astrixCount == 0 &amp;&amp; filter.Length &gt; 0) { return source.Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Equal(selector.Body, Expression.Constant(filter)), selector.Parameters[0] ) ); } // * if (astrixCount == 1 &amp;&amp; filter.Length == 1) return source; // Invalid Filter if (astrixCount &gt; 0) throw new ApplicationException(string.Format("Invalid filter used{0}.", fieldName == null ? "" : " for '" + fieldName + "'")); // Empty string: all results return source; } } </code></pre>
 

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