开发者

linq: groupby with multiple nullable types

I have the following table:

| 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      |                    

|

What I'm trying to do is create the following output that counts the activity by day.

|    Date     |   Set   |   Attended   |   Rescheduled   |   Cancelled   |
|  3/10/2011  |    1    |              |                 |               |
|  3/11/2011  |    1    |      1       |       1         |      2        |

Note that I've defined the fields AppointAttended, AppointCancelled and AppointRescheduled as nullable because there might not be a date for these.

This is what I have so far and I'm struggling with the groupby because I need to group by multiple columns, and it's a nullable type, and I can't get the .Key to work! In other words, I feel really stuck with this:

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.A开发者_运维问答ppointRescheduled.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 is a datetime that's passed in as a parameter and that represents the 1st day of the month that's queried: 3/1/2011 for the month of the March for example.

I get "An anonymous type cannot have multiple properties with the same name" in the groupby statement and the .Key doesn't work so the grouping by day is not working.

If you have any suggestions, that'd be really appreciated.

Thanks.


You can try to explicitly set the right names of the properties of you anonymous type on which you group:

group appnt by new {
                     Set = appnt.AppointSet,
                     Rescheduled = appnt.AppointRescheduled,
                     Attended = appnt.AppointAttended,
                     Cancelled = appnt.AppointCancelled } into daygroups
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜