Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all I'd like to point out that key-value pairs (AKA EAV model) are a poor choice for representing this kind of data, and this question is a perfect example of why - it's much, much harder to search for arbitrary collections of attributes than it is to search for specific properties.</p> <p>Of course, it can still be done:</p> <pre><code>var suitableProducts = from p in products where p.Items.Any(s =&gt; s.Key == "dental" &amp;&amp; s.Value == "true") &amp;&amp; p.Items.Any(s =&gt; s.Key == "therapies" &amp;&amp; s.Value == "false") select p; </code></pre> <p>It's just not as easy to write or efficient as querying on a <code>Product</code> class that actually has <code>dental</code> and <code>therapies</code> properties as opposed to nested attributes.</p> <p>If you the number of items you need to <em>query on</em> can change, then the easiest way to handle that is to chain together filters:</p> <pre><code>var searchItems = ... var result = products; foreach (var searchItem in searchItems) { result = result.Where(p =&gt; p.Items.Any(s =&gt; s.Key == searchItem.Key &amp;&amp; s.Value == searchItem.Value)); } // Do something with result </code></pre> <p>If you're looking for a more "functional" way to do it without chaining then:</p> <pre><code>var suitabilityConditions = searchItems.Select(i =&gt; (Predicate&lt;Product&gt;)(p =&gt; p.Items.Any(s =&gt; s.Key == searchItem.Key &amp;&amp; s.Value == searchItem.Value))); Predicate&lt;Product&gt; match = p =&gt; suitabilityConditions.All(c =&gt; c(p)); var suitableProducts = products.Where(match); </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. 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