C# 分组并仅返回LINQ查询中的最新记录
我有一个表格C# 分组并仅返回LINQ查询中的最新记录,c#,linq,entity-framework,C#,Linq,Entity Framework,我有一个表格AppointmentStatusHistory,格式如下: AppointmentId AppointmentStatusId Date ======================================================== 1 1 2/1/2012 (2nd January) 1 2 2/2/2012 (2nd
AppointmentStatusHistory
,格式如下:
AppointmentId AppointmentStatusId Date
========================================================
1 1 2/1/2012 (2nd January)
1 2 2/2/2012 (2nd February)
我目前正在对此进行查询,以返回给定时间范围内约会的“最近”状态
我的LINQ查询
items = (from t1 in db.AppointmentStatusHistories
from t2 in db.TaskAppointments
.Where(o => (o.ID == t1.AppointmentId))
from t3 in db.Tasks
.Where(o => (o.Task_ID == t2.Parent_Task_ID))
where t1.Timestamp >= first && t1.Timestamp <= last
&& t3.Creator == user
group t1 by t1.AppointmentId into grp
select new UserAppointmentTargetModel
{
AppointmentId = grp.Key,
AppointmentStatusId = grp.OrderByDescending(g => g.Timestamp)
.FirstOrDefault()
.AppointmentStatusId
}
);
items=(来自db.AppointmentStatusHistories中的t1
从t2开始,以db.1为单位
。其中(o=>(o.ID==t1.AppointmentId))
从t3开始,以db.Tasks为单位
。其中(o=>(o.Task\u ID==t2.Parent\u Task\u ID))
其中t1.Timestamp>=first&&t1.Timestamp g.Timestamp)
.FirstOrDefault()
.任命状态ID
}
);
当first=2012年1月1日,last=2012年2月1日时,使用上述函数返回'1'的状态
要求
我希望有人能给我一些建议,修改这一点,以满足以下条件:
- 如果最新状态在当前期间内,包括该记录李>
- 如果不是,则从结果集中忽略它
最后一部分移动到分组/winnerpicking之后
db.AppointmentStatusHistories
.Where(ash => first <= ash.TimeStamp) //omit all records that are before the range
.Where(ash => ash.Appointment.TaskAppointments.Any(ta => ta.Task.Creator == user))
.GroupBy(ash => ash.AppointmentId)
.Select(g => g.OrderByDescending(ash => ash.TimeStamp).First()) //pick a winner
.Where(ash => ash.TimeStamp <= last) //omit winners that are after the range
.Select(ash => new UserAppointmentTargetModel()
{
AppointmentId = ash.AppointmentId,
AppoinementStatusId = ash.AppointmentStatus,
Date = ash.Timestamp
}
db.AppointmentStatusHistories
.Where(ash=>first ash.Appointment.taskappointment.Any(ta=>ta.Task.Creator==user))
.GroupBy(ash=>ash.AppointmentId)
.Select(g=>g.OrderByDescending(ash=>ash.TimeStamp).First())//选择赢家
.Where(ash=>ash.TimeStamp new UserAppointmentTargetModel()
{
AppointmentId=ash.AppointmentId,
AppointStatusID=ash.AppointStatus,
日期=灰烬。时间戳
}
(上述的强制性查询理解语法形式)
来自db.AppointStatusHistories中的ash的
其中第一个ta.Task.Creator==用户)
按ash.AppointmentId将ash分组为g
让winner=g.OrderByDescending(ash=>ash.TimeStamp)
其中winner.TimeStamp=带日期。这只会导致悲伤。您只需将过滤的最后一部分移动到分组/winnerpicking之后即可
db.AppointmentStatusHistories
.Where(ash => first <= ash.TimeStamp) //omit all records that are before the range
.Where(ash => ash.Appointment.TaskAppointments.Any(ta => ta.Task.Creator == user))
.GroupBy(ash => ash.AppointmentId)
.Select(g => g.OrderByDescending(ash => ash.TimeStamp).First()) //pick a winner
.Where(ash => ash.TimeStamp <= last) //omit winners that are after the range
.Select(ash => new UserAppointmentTargetModel()
{
AppointmentId = ash.AppointmentId,
AppoinementStatusId = ash.AppointmentStatus,
Date = ash.Timestamp
}
db.AppointmentStatusHistories
.Where(ash=>first ash.Appointment.taskappointment.Any(ta=>ta.Task.Creator==user))
.GroupBy(ash=>ash.AppointmentId)
.Select(g=>g.OrderByDescending(ash=>ash.TimeStamp).First())//选择赢家
.Where(ash=>ash.TimeStamp new UserAppointmentTargetModel()
{
AppointmentId=ash.AppointmentId,
AppointStatusID=ash.AppointStatus,
日期=灰烬。时间戳
}
(上述的强制性查询理解语法形式)
来自db.AppointStatusHistories中的ash的
其中第一个ta.Task.Creator==用户)
按ash.AppointmentId将ash分组为g
让winner=g.OrderByDescending(ash=>ash.TimeStamp)
其中winner.TimeStamp=带日期。这只会导致悲伤。您如何确定日期状态是否在当前期间?@Aducci使用“日期”字段操作任命状态表如何确定日期状态是否在当前期间?@Aducci使用“日期”字段操作任命状态表非常感谢您的清晰回答和出色的回答解释。由于我永远不知道的原因,TaskAppointment未加入AppointmentStatusHistories。加入这个的最有效的地方是哪里?@Nick我已经更新了查询,以查看AppointmentStatusHistories的Appointment属性。导航属性不会直接关联两个子表(约会表),这是有道理的。非常感谢您的清晰回答和精彩解释。由于我永远不知道的原因,TaskAppointment未加入AppointmentStatusHistories。加入这个的最有效的地方是哪里?@Nick我已经更新了查询,以查看AppointmentStatusHistories的Appointment属性。导航属性不会直接关联(约会)的两个子表,这是有道理的。