LINQ groupby语句带键

fre*_*hie 7 .net c# linq group-by

我的查询的语法很好但不是输出,这真的很奇怪.

我有下表:

| AppointID | UserID | AppointSet | AppointResolved |  AppointStatus | AppointmentDatetime
|     1     |   1    |  3/1/2011  |   3/1/2011      |      1         |     3/15/2011
|     2     |   1    |  3/2/2011  |   3/5/2011      |      4         |     3/16/2011
|     3     |   1    |  3/2/2011  |   3/11/2011     |      2         |     3/11/2011
|     4     |   1    |  3/3/2011  |   3/7/2011      |      3         |     3/25/2011
Run Code Online (Sandbox Code Playgroud)

ApponintStatus是一个字节,其中1表示设置,2表示有人参加,3表示重新安排,4表示已取消.AppointDatetime是设置约会的日期.

我要做的是创建以下输出,按天计算活动.

|    Date     |   Set   |   Attended   |   Rescheduled   |   Cancelled   |
|  3/1/2011   |    1    |              |                 |               |
|  3/2/2011   |    2    |              |                 |               |
|  3/3/2011   |    1    |              |                 |               |
|  3/5/2011   |         |              |                 |      1        |
|  3/7/2011   |         |      1       |                 |               |
|  3/11/2011  |         |              |        1        |               |
Run Code Online (Sandbox Code Playgroud)

这就是我到目前为止所拥有的.TheDate是我要查询的月份内的日期(即通过3月4日并应返回March表格)

var r = from appnt in MyDC.LeadsAppointments
        where appnt.UserID == TheUserID
        where (appnt.AppointResolved.Year == TheDate.Year && appnt.AppointResolved.Month == TheDate.Month) ||
        (appnt.AppointSet.Year == TheDate.Year && appnt.AppointSet.Month == TheDate.Month)
        group appnt by appnt.AppointResolved.Date into daygroups
        select new ViewMonthlyActivityModel()
        {
            ViewDate = (from d in daygroups
                        select daygroups.Key.Date).First(), // Problem here: need to get dates for instances where an appointment is set but none are resolved

            CountTotalSetOnDay = (from c in daygroups
                                  where c.AppointSet.Date == daygroups.Key // Problem here
                                  select c.AppointID).Count(),

            CountAttendedOnDay = (from c in daygroups
                                  where c.AppointResolved.Date == daygroups.Key.Date
                                  where c.AppointStatus == 2
                                  select c.AppointID).Count(),
Run Code Online (Sandbox Code Playgroud)

其中一个问题是CountTotalSetOnDay只返回当天设置和解析的计数; 另一个问题是ViewDate需要返回所有日期:有没有设置约会但约会参加,重新安排或取消的日期,反之亦然,有约会设置但没有解决的日期.

现在我用2个查询来运行它,我加入结果:一个查询返回约会集,另一个返回已解决的约会.但是,我仍然坚持使用一次查询中的一个查询解决方案.

有什么建议?

谢谢.

fre*_*hie 0

我想带有 2 个读取查询的解决方案工作得很好,并且现在可以使用。