My application often needs to group a table and then return a row with the maximum value for that group. This is pretty easy to do in LINQ:
myTable.GroupBy(r => r.FieldToGroupBy)
.Select(r => r.Max(s => s.FieldToMaximize))
.Join(
myTable,
r => r,
r => r.FieldToMaximize,
(o, i) => i)
Now suppose I want to distract this with my own method. I tried to write this:
public static IQueryable<TSource>
SelectMax<TSource, TGroupKey, TMaxKey>(
this IQueryable<TSource> source,
Expression<Func<TSource, TGroupKey>> groupKeySelector,
Expression<Func<TSource, TMaxKey>> maxKeySelector)
where TMaxKey : IComparable
{
return source
.GroupBy(groupKeySelector)
.Join(
source,
g => g.Max(maxKeySelector),
r => maxKeySelector(r),
(o, i) => i);
}
Unfortunately, this is not compiled: maxKeySelector is an expression (therefore, you cannot call it on r, and you cannot even pass it to Max. Therefore, I tried to rewrite it by making maxKeySelector a function, not an expression:
public static IQueryable<TSource>
SelectMax<TSource, TGroupKey, TMaxKey>(
this IQueryable<TSource> source,
Expression<Func<TSource, TGroupKey>> groupKeySelector,
Func<TSource, TMaxKey> maxKeySelector)
where TMaxKey : IComparable
{
return source
.GroupBy(groupKeySelector)
.Join(
source,
g => g.Max(maxKeySelector),
r => maxKeySelector(r),
(o, i) => i);
}
. : " , " "." , : maxKeySelector Max().
? LINQ to SQL, , , .