score:2
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:
private Func<TestGuy,int> GetAverageField(SomeEnum someCondition)
to this:
private Expression<Func<TestGuy,int>> GetAverageField(SomeEnum someCondition)
and then pass the result of the call into the relevant query method, e.g.
var results = query.Select(GetAverageField(fieldToAverageBy));
In this case, however, you'll need to build the whole expression tree up for the Select
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.
If you're using .NET 4, dynamic typing may help you, although you'd pay the price of not having static typing any more, of course.
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.
Marc Gravell may be able to help more on this - it does sound like the kind of thing which should be possible, but I'm at a loss as to how to do it elegantly at the moment.
score:0
Eh? the parameter to Queryable.Average is not Func<T, U>
. It's Expression<Func<T, U>>
The way to do this is:
private Expression<Func<TestGuy,int>> GetAverageExpr(SomeEnum someCondition)
{
switch(someCondition)
{
case SomeEnum.Interesting:
return tg => tg.InterestingValue;
case SomeEnum.Other:
return tg => tg.OtherValue;
}
throw new InvalidOperationException("Not in my example!");
}
Followed by:
Expression<Func<TestGuy, int>> averageExpr = GetAverageExpr(someCondition);
var byCity = testGuys
.GroupBy(c => c.City)
.Select(g => new { City = g.Key, Avg = g.Average(averageExpr) });
Source: stackoverflow.com
Related Articles
- NHibernate 3 LINQ - how to create a valid parameter for Average()
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- How do you use an Nullable<T> parameter with an nHibernate Linq Expression?
- NHibernate LINQ query performance, which code fragment is better?
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- source code for LINQ 101 samples
- Generic parameter in NHibernate Linq query
- Could not resolve property for Average linq to NHibernate mapped object
- How to dynamically create linq code at runtime and get results
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- linq create average from different columns
- c# Linq or code to extract groups from a single list of source data
- EF 6, LINQ how to create where inside include with conditional parameter
- NHibernate linq - pass lambda function as a parameter to another lambda function
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Create a list from two object lists with linq
- Create batches in linq
- NHibernate vs LINQ to SQL
- Linq code to select one item
- Create a Tuple in a Linq Select
- Linq Query keeps throwing "Unable to create a constant value of type System.Object....", Why?
- Fetch vs FetchMany in NHibernate Linq provider
- How are people unit testing code that uses Linq to SQL
- Using Linq to objects, how to create an empty dictionary of <string, string> easily?
- How to create LINQ Expression Tree to select an anonymous type
- Linq for NHibernate and fetch mode of eager loading
- How can i fix this error? c# asp linq
- Stored Procedure returning an int
- How to pass dictionary values without having to iterate using a loop in c#
- Get the name of a JObject in Json.Net
- Returning from a DbSet 3 tables without the error "cannot be inferred from the query"
- Refining linq query
- How to change value of child element based on attribute of child and parent?
- How to combine two linq queries using Union
- Get next value after FirstOrDefault
- Dynamic queries in LINQ
- Sort on anonymous type in DBQuery
- XML to LINQ how to take exact informations?
- Custom Nullable<T> Extension Methods and SelectMany
- Select top N with record from DataTable with some sorting using Linq
- Func delegate working unexpectedly
- ObservableCollection not updated when doing a second LINQ query?
- Initializing Lookup<int, string>
- How to introduce Let keyword inside Linq statement with Group by
- Converting LINQ query syntax to Lambda notation manually
- Using Linq for Object Dataset Processing