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 Query
- NHibernate 3 LINQ - how to create a valid parameter for Average()
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to create an efficient data structure for two-dimensional LINQ operations?
- linq - how do you do a query for items in one query source that are not in another one?
- How to create a custom store expression for my linq queries
- How do you use an Nullable<T> parameter with an nHibernate Linq Expression?
- How to create regex for mathing linq statement NOT contains where clause?
- How use if condition for nullable boolean in linq to NHibernate
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- How can I refactor this code for LINQ filtering?
- How to create a LINQ To SQL datacontext for large DB using SQLMetal when it generates a 12MB DataContext.cs file?
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- How to get PEX to automatically generate inputs for code involving LINQ
- How use of select for associations in linq to NHibernate
- how to write LINQ to objects equivalent code for this code
- source code for LINQ 101 samples
- How to create a breadcrumb for categories and subcategories using linq
- Could not resolve property for Average linq to NHibernate mapped object
- How do I pass a parameter from the select list into a function for joining a linq query?
- How to dynamically create linq code at runtime and get results
- How to create a dynamic linq query for filter based on ria service?
- How do you declare a by-reference parameter variable for use in a Linq query?
- How To create linq Expression dynamically in c# for Contains
- How to create a reusable function for LINQ where clause with extra param
- C# LINQ or for loop How to get a data source from a db?
- How can I create an Lambda Expression for Linq in the Where clause for two tables after the Join?
- How do you create a nested LINQ Grouping / Lookups for a Collection of Objects
- How to create Linq Expression for DateTime.Date
- how to create a left join query for a many to many related table with LINQ
- How do I use Generics to create multiple/nested SelectMany joins in Linq for DocumentDB
More Query from same tag
- How orderBy dynamically, based on navigation property
- Handling null values for C# linq where query
- How to sort list on multiple properties in one line of code in Linq
- MySQL in MVC 3: Specified method is not supported
- LINQ query to WebControl.Controls
- Set EntityCollection in a Linq projection
- Converting List<string> to EntityFramework column/field list
- Getting distinct values from a list of objects with objects
- Get properties in LINQ inside group by clause
- Correct syntax for creating an array from a foreach loop
- Why the number of calls generated by LINQ FirstOrDefault or First
- RavenDB full-text search dictionaries using IRavenQueryable
- LINQ orderby on date field in descending order
- C# - Linq - XPathSelectElement returns the same results everytimes
- LINQ Select if not null
- EntityFrameworkCore GroupBy Select FirstOrDefault() SystemInvalidOperationException
- search database table record using two parameters in linq
- Remove From Duplicate Starting Names From List Linq
- Combining resultset in C# using Linq
- How to avoid the "System.InvalidOperationException: The client projection contains a reference to a constant expression" error?
- When using LINQ, what is the difference between && and multiple where clauses?
- Explain performance differences between different LINQ statements
- How to do a Many to Many join in Linq
- Applying child collections to matching parents in the parent collection
- Linq to List: Is there a way to include all the columns after applying a groupby on list
- Filtering List from a List
- Convert to date Format
- Ordering ObservableCollection with Linq and IComparer
- How can I improve the performance of LinqToSql queries that use EntitySet properties?
- Linq query list of blob, memory usage