Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Solution 1</h2> <p>This solution doesn't strip out the invoke statements which I assume you are trying to do by calling "Expand".</p> <p>change the result of "forTargetId(String id)" to</p> <pre><code>predicate.And(m =&gt; fc.Invoke(m) == id); </code></pre> <p>When the expression is compiled in the where clause it will know that it needs to pass "m" to the fc expression above. </p> <p>My first tip came when I changed </p> <pre><code>predicate.And(m =&gt; fc.Invoke(m) == id).Expand(); </code></pre> <p>to</p> <pre><code>predicate.And(n =&gt; fc.Invoke(n) == id).Expand(); </code></pre> <p>and I could see that n was not being pass along at all.</p> <p>I tested out this change by manipulating the Main method as follows</p> <pre><code>static void Main(string[] args) { var barModel = new BarModel(); barModel.list.Add(new Model() { key = "1", value = "One" }); barModel.list.Add(new Model() { key = "2", value = "Two" }); barModel.list.Add(new Model() { key = "some", value = "Three" }); string id = "some"; Console.WriteLine(barModel.subFor(id).ToString()); // output: m =&gt; (True AndAlso (m.key == value(ConsoleApplication2.Bar`1+&lt;&gt;c__DisplayClass0[ConsoleApplication2.Model]).id)) Console.ReadKey(); var subworkitems = barModel.list.Where(barModel.subFor(id).Compile()); // Exception {"variable 'm' of type 'ConsoleApplication2.Model' referenced from scope '', but it is not defined"} foreach (var si in subworkitems) { Console.WriteLine(si.key); Console.WriteLine(si.value); } Console.WriteLine(subworkitems.ToString()); Console.ReadKey(); } </code></pre> <h2>Solution 2</h2> <p>This solution does remove the Invoke statements with the use of the Expand method but changes the way that you are anding the statements together.</p> <p>The "Expand" function is advertised as following on the LinqKit website.</p> <pre><code>Expression&lt;Func&lt;Purchase, bool&gt;&gt; criteria1 = p =&gt; p.Price &gt; 1000; Expression&lt;Func&lt;Purchase, bool&gt;&gt; criteria2 = p =&gt; criteria1.Invoke(p) || p.Description.Contains("a"); Console.WriteLine(criteria2.Expand().ToString()); // p =&gt; ((p.Price &gt; 1000) || p.Description.Contains("a")) </code></pre> <p>Notice that they aren't using the "And" method to put these things together but instead they are "daisy chaining" the calls together. </p> <p>Instead of </p> <pre><code>Expression&lt;Func&lt;TModel, bool&gt;&gt; predicate = m =&gt; true; var result = predicate.And(m =&gt; fc.Invoke(m) == id).Expand(); </code></pre> <p>Do this</p> <pre><code>Expression&lt;Func&lt;TModel, bool&gt;&gt; predicate = m =&gt; fc.Invoke(m) == id &amp;&amp; true; return predicate.Expand(); </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.
    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