Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat am I missing in this chain of predicates?
    primarykey
    data
    text
    <p><strong>NOTE</strong>: Right before posting this question it occurred to me there's a better way of doing what I was trying to accomplish (and I feel pretty stupid about it):</p> <pre><code>IEnumerable&lt;string&gt; checkedItems = ProductTypesList.CheckedItems.Cast&lt;string&gt;(); filter = p =&gt; checkedItems.Contains(p.ProductType); </code></pre> <p>So OK, yes, I already realize this. However, I'm posting the question anyway, because I still don't quite get why what I was (stupidly) <strong>trying</strong> to do wasn't working.</p> <hr> <p>I thought this would be extremely easy. Turns out it is giving me quite a headache.</p> <p>The basic idea: display all the items whose <code>ProductType</code> property value is checked in a <code>CheckedListBox</code>.</p> <p>The implementation:</p> <pre><code>private Func&lt;Product, bool&gt; GetProductTypeFilter() { // if nothing is checked, display nothing Func&lt;Product, bool&gt; filter = p =&gt; false; foreach (string pt in ProductTypesList.CheckedItems.Cast&lt;string&gt;()) { Func&lt;Product, bool&gt; prevFilter = filter; filter = p =&gt; (prevFilter(p) || p.ProductType == pt); } return filter; } </code></pre> <p>However, say the items "Equity" and "ETF" are both checked in <code>ProductTypesList</code> (a <code>CheckedListBox</code>). Then for some reason, the following code only returns products of type "ETF":</p> <pre><code>var filter = GetProductTypeFilter(); IEnumerable&lt;Product&gt; filteredProducts = allProducts.Where(filter); </code></pre> <p>I guessed it might have had something to do with some self-referencing messiness where <code>filter</code> is set to, essentially, itself <em>or</em> something else. And I thought that maybe using ...</p> <pre><code>filter = new Func&lt;Product, bool&gt;(p =&gt; (prevFilter(p) || p.ProductType == pt)); </code></pre> <p>...would do the trick, but no such luck. Can anybody see what I am missing here?</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.
 

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