1

How can I change this method so it also returns Max(t.StartTime) and Min(t.StartTime) with only using it in one line as below?

 public IQueryable<Timetable> GetTimetables()
    {
        return from t in _entities.Timetables
               select t;
    }

/M

jdehaan
  • 19,700
  • 6
  • 57
  • 97
Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109

2 Answers2

1

Off the top of my head (read: untested) and presuming StartTime is non-nullable:

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)
           };
}

This gets considerably more complicated when StartTime is nullable.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
-1

I don't know if this is applicable to Entity framework but with linq it's something like this

 public IQueryable<Timetable> GetTimetables()
    {
        return from t in _entities.Timetables
               select new {maxt = Max(t.StartTime), mint = Min(t.StartTime)};
    }
Omu
  • 69,856
  • 92
  • 277
  • 407