Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You won't be able to call a "local" method within your lambda expression. If this were a simple non-nested clause, it would be relatively simple - you'd just need to change this:</p> <pre><code>private Func&lt;TestGuy,int&gt; GetAverageField(SomeEnum someCondition) </code></pre> <p>to this:</p> <pre><code>private Expression&lt;Func&lt;TestGuy,int&gt;&gt; GetAverageField(SomeEnum someCondition) </code></pre> <p>and then pass the result of the call into the relevant query method, e.g.</p> <pre><code>var results = query.Select(GetAverageField(fieldToAverageBy)); </code></pre> <p>In this case, however, you'll need to build the whole expression tree up for the <code>Select</code> clause - the anonymous type creation expression, the extraction of the Key, and the extraction of the average field part. It's not going to be fun, to be honest. In particular, by the time you've built up your expression tree, that's not going to be statically typed in the same way as a normal query expression would be, due to the inability to express the anonymous type in a declaration.</p> <p>If you're using .NET 4, dynamic typing <em>may</em> help you, although you'd pay the price of not having static typing any more, of course.</p> <p>One option (horrible though it may be) would be try to use a sort of "template" of the anonymous type projection expression tree (e.g. always using a single property), and then build a copy of that expression tree, inserting the right expression instead. Again, it's not going to be fun.</p> <p>Marc Gravell may be able to help more on this - it does sound like the kind of thing which <em>should</em> be possible, but I'm at a loss as to how to do it elegantly at the moment.</p>
 

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