I am already scratching my head a bit over this. Say I'm making an extension method to group the list of items by date, I want to change the possible grouping so that the results can be grouped by day, week or month.
I came up with below, but I keep getting the following error:
Method 'System.Object DynamicInvoke (System.Object [])' does not support SQL translation
Method:
public static IQueryable<IGrouping<MonthDateGroup, T>> GroupByDate<T>( this IQueryable<T> items, DateGroupFrequency grouping, Expression<Func<T, DateTime?>> func) { var selector = func.Compile(); IQueryable<IGrouping<MonthDateGroup, T>> grouped = null; if (grouping == DateGroupFrequency.Daily) { grouped = from a in items let date = selector(a).Value group a by new MonthDateGroup { Day = date.Day, Month = date.Month, Year = date.Year } into g select g; }
I assume that using Func in the query causes an error, is it possible to create such code?
PS I'm still trying to wrap my head around Func and expression :)
source share