【发布时间】:2011-03-10 07:33:47
【问题描述】:
我有下表:
| AppointID | UserID | AppointSet | AppointAttended | AppointCancelled | AppointRescheduled |
| 1 | 1 | 2/15/2011 | | 3/11/2011 | |
| 2 | 1 | 2/17/2011 | | | 3/11/2011 |
| 3 | 1 | 3/11/2011 | 3/11/2011 | | |
| 4 | 1 | 3/10/2011 | | 3/11/2011 |
|
我要做的是创建以下输出,按天计算活动。
| Date | Set | Attended | Rescheduled | Cancelled |
| 3/10/2011 | 1 | | | |
| 3/11/2011 | 1 | 1 | 1 | 2 |
请注意,我已将 ApppointAttended、ApppointCancelled 和 ApppointRescheduled 字段定义为可为空,因为这些字段可能没有日期。
这是我到目前为止所拥有的,我正在努力解决 groupby 因为我需要按多列分组,而且它是一个可为空的类型,我无法让 .Key 工作!换句话说,我真的很坚持这个:
var OutputMonthlyActivity = from appnt in MyDC.LeadsAppointments
where appnt.UserID == TheUserID
where (appnt.AppointSet.Month == TheDate.Month && appnt.AppointSet.Year == TheDate.Year) ||
(appnt.AppointAttended.Value.Month == TheDate.Month && appnt.AppointAttended.Value.Year == TheDate.Year) ||
(appnt.AppointRescheduled.Value.Month == TheDate.Month && appnt.AppointRescheduled.Value.Year == TheDate.Year) ||
(appnt.AppointCancelled.Value.Month == TheDate.Month && appnt.AppointCancelled.Value.Year == TheDate.Year)
group appnt by new { appnt.AppointSet, appnt.AppointRescheduled, appnt.AppointAttended, appnt.AppointCancelled } into daygroups
where daygroups.Count() > 1
select new ViewMonthlyActivityModel()
{
ViewDate = daygroups.Key,
CountTotalSetOnDay = (from c in daygroups
where c.AppointSet.Date == daygroups.Key
select c.AppointID).Count(),
TheDate 是作为参数传入的日期时间,它表示所查询的月份的第一天:例如,3 月份的 3/1/2011。
我在 groupby 语句中得到“一个匿名类型不能有多个同名的属性”,并且 .Key 不起作用,因此按天分组不起作用。
如果您有任何建议,我们将不胜感激。
谢谢。
【问题讨论】: