Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Check this article out to do what you want:<br/> <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="noreferrer">http://www.albahari.com/nutshell/predicatebuilder.aspx</a><br/><br/> This works like a dream. I essentially cut and pasted their code and got this back (with my own data-scheme of course):</p> <pre><code>SELECT [t0].[Id], [t0].[DateCreated], [t0].[Name] ... FROM [dbo].[Companies] AS [t0] WHERE ([t0].[Name] LIKE @p0) OR ([t0].[Name] LIKE @p1) </code></pre> <p>Here is the code I ran for the proof of concept:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; namespace PredicateTest { class Program { static void Main(string[] args) { DataClasses1DataContext dataContext = new DataClasses1DataContext(); Program p = new Program(); Program.SearchCompanies("test", "test2"); var pr = from pi in dataContext.Companies.Where(Program.SearchCompanies("test", "test2")) select pi; } DataClasses1DataContext dataContext = new DataClasses1DataContext(); public static Expression&lt;Func&lt;Company, bool&gt;&gt; SearchCompanies( params string[] keywords) { var predicate = PredicateBuilder.False&lt;Company&gt;(); foreach (string keyword in keywords) { string temp = keyword; predicate = predicate.Or(p =&gt; p.Name.Contains(temp)); } return predicate; } } public static class PredicateBuilder { public static Expression&lt;Func&lt;T, bool&gt;&gt; True&lt;T&gt;() { return f =&gt; true; } public static Expression&lt;Func&lt;T, bool&gt;&gt; False&lt;T&gt;() { return f =&gt; false; } public static Expression&lt;Func&lt;T, bool&gt;&gt; Or&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; expr1, Expression&lt;Func&lt;T, bool&gt;&gt; expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast&lt;Expression&gt;()); return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt; (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); } public static Expression&lt;Func&lt;T, bool&gt;&gt; And&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; expr1, Expression&lt;Func&lt;T, bool&gt;&gt; expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast&lt;Expression&gt;()); return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt; (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters); } } } </code></pre> <p>I'd suggest going to the site for the code and explanation.</p> <p>(I am leaving the first answer because it works well if you need an IN statement)</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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