Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all the 'blahs' (classes) that you will be filtering have the same structure, you can use a simple method like this. The main difference is that it returns an Expression that Linq should be able to parse and it brings in the whole instance and filters on Name instead of bringing in just the string name.</p> <pre><code> public static Expression&lt;Func&lt;T, bool&gt;&gt; BuildStringMatch&lt;T&gt;(string searchFor) where T : IHasName { return b =&gt; string.IsNullOrEmpty(searchFor) || (b.Name != null &amp;&amp; (b.Name.Trim().ToLower().StartsWith(searchFor.Trim().ToLower()) || b.Name.Contains(" " + searchFor))); } </code></pre> <p>You can use that method like this:</p> <pre><code> IQueryable&lt;Blah&gt; blahs = query.Where(BuildStringMatch&lt;Blah&gt;(searchText)); </code></pre> <p>That assumes all your classes that you'd want to filter on implement some interface such as:</p> <pre><code> public interface IHasName { string Name { get; } } </code></pre> <p>If you want to be filtering on different properties, I don't think that's something you can do with simple code like this. I believe you'll need to build the Expression yourself with reflection (or with the help of a library that uses reflection) - it's still possible but much more difficult.</p> <p>Edit: It sounds like you need dynamic behavior, so I borrowed some logic from <a href="https://stackoverflow.com/users/76217/dtb">dtb</a>'s answer to <a href="https://stackoverflow.com/questions/3431617/creating-dynamic-predicates-passing-in-property-to-a-function-as-parameter/3431670#3431670">this question</a> and came up with this:</p> <pre><code>public static Expression&lt;Func&lt;T, bool&gt;&gt; BuildStringMatch&lt;T&gt;(Expression&lt;Func&lt;T, string&gt;&gt; property, string searchFor) { var searchForExpression = Expression.Constant(searchFor, typeof(string)); return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.OrElse( Expression.Call(typeof(string), "IsNullOrEmpty", null, searchForExpression), Expression.AndAlso( Expression.NotEqual(property.Body, Expression.Constant(null, typeof(string))), Expression.OrElse( Expression.Call(Expression.Call(Expression.Call(property.Body, "Trim", null), "ToLower", null), "StartsWith", null, Expression.Call(Expression.Call(searchForExpression, "Trim", null), "ToLower", null)), Expression.Call(property.Body, "Contains", null, Expression.Call(typeof(string), "Concat", null, Expression.Constant(" "), searchForExpression)) ) ) ), property.Parameters ); } </code></pre> <p>You would use it like:</p> <pre><code> IQueryable&lt;Blah&gt; blahs2 = query.Where(BuildStringMatch&lt;Blah&gt;(b =&gt; b.Name, searchText)); </code></pre> <p>It's long and verbose but you can see how it's similiar to the original method written in straight C# code. Note: I didn't test this code, so there could be a few small problems - but that is the general idea.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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