Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow noreferrer">PredicateBuilder</a> (from the authors of LinqPad) you can build up a query from a set of conditions not known at compile time. The following should be sufficient:</p> <pre><code>var predicate = PredicateBuilder.True&lt;Product&gt;(); foreach (Suitability criteria in searchCriteria) { string tempKey = criteria.Key; string tempValue = criteria.Value; predicate = predicate.And(p =&gt; p.Items.Any(s =&gt; s.Key == tempKey &amp;&amp; s.Value == tempValue)); } return dataContext.Products.Where(predicate.Compile()); </code></pre> <p>UPDATE: Heres some sample code that I have tested which works using an IEnumerable as the source, the resulting set <code>productsResult</code> contains the first and second products in <code>products</code> list:</p> <pre><code>var searchCriteria = new List&lt;Suitability&gt;() { new Suitability() { Key="a", Value="b" }, new Suitability() { Key="a", Value="c" } }; var products = new List&lt;Product&gt;() { new Product() { Items = new List&lt;Suitability&gt;() { new Suitability() { Key="a", Value="b" }, new Suitability() { Key="a", Value="c" }} }, new Product() { Items = new List&lt;Suitability&gt;() { new Suitability() { Key="a", Value="b" }, new Suitability() { Key="a", Value="c" }, new Suitability() { Key="b", Value="c" }} }, new Product() { Items = new List&lt;Suitability&gt;() { new Suitability() { Key="c", Value="d" }} } }; var predicate = PredicateBuilder.True&lt;Product&gt;(); foreach (Suitability criteria in searchCriteria) { string tempKey = criteria.Key; string tempValue = criteria.Value; predicate = predicate.And(p =&gt; p.Items.Any( s =&gt; s.Key == tempKey &amp;&amp; s.Value == tempValue)); } IEnumerable&lt;Product&gt; productsResult = products.Where(predicate.Compile()); </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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