我已经为这个问题摸不着头脑了一段时间。假设我做了一个扩展方法,要按日期对项目列表进行分组,我想更改可能的分组,以便结果可以按天、周或月进行分组。
我想到了以下内容,但我不断收到以下错误:
Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL
方法:
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;
}
//Rest of groupings...
我假设在查询中使用 Func 是导致错误的原因,是否可以生成这样的代码?
附言我仍在努力思考 Func 和表达式 :)
最佳答案
组语法不应该是:
group a by new MonthDateGroup
{
a.Day = date.Day, a.Month = date.Month, a.Year = date.Year
}
关于c# - LINQ to SQL - 按天/周/月分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679466/