C# 在LinQ组中使用DateTime.Quarter
我需要在LinQ表达式中按季度分组。以下是年份代码:C# 在LinQ组中使用DateTime.Quarter,c#,linq,group-by,C#,Linq,Group By,我需要在LinQ表达式中按季度分组。以下是年份代码: temp = rmds.GroupBy(a => new { a.reportinggroup, a.timestep.Year }) .Select(g => ....}).ToList(); 现在我想用一个季度表达式替换GroupBy中的DateTime.Year。以下是我的扩展方法: public static int quarter(this DateTime @this) { return
temp = rmds.GroupBy(a => new { a.reportinggroup, a.timestep.Year })
.Select(g => ....}).ToList();
现在我想用一个季度表达式替换GroupBy中的DateTime.Year
。以下是我的扩展方法:
public static int quarter(this DateTime @this)
{
return (int) Math.Ceiling((double)@this.Month / 3);
}
但如果我在LinQ中替换此选项:
temp = rmds.GroupBy(a => new { a.reportinggroup, a.timestep.quarter() });
我发现以下错误:
错误1无效的匿名类型成员声明符。匿名类型成员必须使用成员分配、简单名称或成员访问权限声明
为什么??请告诉我按季度分组需要使用什么?错误消息告诉您选项是什么。
a.timestep.quarter()
部分写入时无效,因为:
- 它不是成员分配(例如
)foo=a.timestep.quarter()
- 它不是一个简单的名称(例如,只是
)timestep
- 它不是成员访问权限(例如,
是)a.reportinggroup
temp = rmds.GroupBy(a => new { a.reportinggroup, quarter = a.timestep.quarter() })
这里的区别是什么???
错误消息告诉您选项是什么。
这些天回答了很多SO问题:)@Neel:区别是季度=
。