【问题标题】:linq: groupby with multiple nullable typeslinq:具有多个可为空类型的 groupby
【发布时间】: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 不起作用,因此按天分组不起作用。

如果您有任何建议,我们将不胜感激。

谢谢。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    您可以尝试为您分组的匿名类型的属性显式设置正确的名称:

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

    【讨论】:

    • 好的,这清除了语法错误。现在我遇到了 ViewDate = daygroups.Key 的问题。如果我添加诸如 = daygroups.Key.Set 之类的东西,它就会起作用:它将按该键分组。但是,无论如何,我如何确保我得到一个日期(即,可能是 Set = null 然后是什么进入 ViewDate?)
    • 我得到了 ViewDate 行:(从 d in daygroups select d.AppointDatetime.Date).First(),问题是当我发送 2011 年 3 月 1 日作为 TheDate 时,我得到从 2 月 26 日到 4 月 2 日的所有约会。知道为什么吗?
    • 语法有效,但输出很奇怪:我得到的日期不在月份内,结果是 1 或 0。
    • 可能是因为 group by 语句无效导致逻辑错误。我不认为这是正确的。如果您按每一列分组,然后使用union 语句将所有结果合并到一个表中怎么办?
    • 感谢您的调查。我在这里重新表述了这个问题,因为我对查询进行了一些改进。 stackoverflow.com/questions/5254099/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多