我的查詢語法很好,但不是輸出,它真的很奇怪。LINQ groupby statement with key
我有以下表:
| 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
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 | |
這是我到目前爲止。 TheDate是我查詢一個月內的日期(即,通過3月4日,應退還三月表)
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(),
一個問題是,CountTotalSetOnDay將返回只有那些設置和計數同一天解決;另一個問題是ViewDate需要返回所有的日期:有沒有設置約會但約會出席,重新安排或取消的日期,反之亦然,有約會設置但沒有解決的日期。
現在我有2個查詢運行此,我加入了結果:一個查詢返回設置的約會和其他收益的任命得到解決。然而,我仍然堅持在一次閱讀中一個查詢的解決方案。
有什麼建議嗎?
謝謝。
是的,這就是我堅持的問題。而且,還有一些情況是有約會的日期,但沒有約定的約會。 – frenchie 2011-03-10 21:49:55
這令人困惑;這個var約會取走了哪裏? – frenchie 2011-03-10 21:55:11
這只是一個示例,向您展示如何從表條目中獲取「設置」列。如果您對輸出表中的每個日期和每個必填字段都做同樣的事情,則應該擁有完成目標所需的所有信息。 – tugudum 2011-03-10 21:59:05