如何更改此方法,以便它还返回Max(t.StartTime)和Min(t.StartTime),只需在一行中使用它,如下所示?
public IQueryable<Timetable> GetTimetables()
{
return from t in _entities.Timetables
select t;
}
/ M
答案 0 :(得分:1)
离开我的头顶(读取:未经测试)并假设StartTime不可为空:
public class TimetableWithMaxMin
{
public Timetable Timetable { get; set; }
public DateTime Max { get; set; }
public DateTime Min { get; set; }
}
public IQueryable<TimetableWithMaxMin> GetTimetables()
{
return from t in _entities.Timetables
select new TimetableWithMaxMin
{
Timetable = t,
Max = _entities.Timetables.Max(t => t.StartTime),
Min = _entities.Timetables.Min(t => t.StartTime)
};
}
当StartTime可以为空时,这会变得相当复杂。
答案 1 :(得分:0)
我不知道这是否适用于Entity框架,但是使用linq就是这样的
public IQueryable<Timetable> GetTimetables()
{
return from t in _entities.Timetables
select new {maxt = Max(t.StartTime), mint = Min(t.StartTime)};
}