0

Hi i have a linq query below

var Free = (from row in dt.AsEnumerable() where row.Field("AppointmentType") == "FreeTime" select new{ row.Field("BookedDate") row.Field("TravelTime")}).Min()

what i want to do is have a minimum on the travelTime field and im not sure on how to do it i have looked on google and also on the msdn site but i cant seem to make head or tail of it

does anyone have any ideas??

Many thanks

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
kevinw
  • 103
  • 1
  • 2
  • 12

1 Answers1

4

You could use an overload for Enumerable.Min():

var Free = (
  from row in dt.AsEnumerable()
  where row.Field("AppointmentType") == "FreeTime"
  select new {
    BookedDate = row.Field("BookedDate"),
    TravelTime = row.Field("TravelTime")
  }
).Min(x => x.TravelTime);

Actually, there is no need to look at the BookedDate and you should instead use

var Free = (
  from row in dt.AsEnumerable()
  where row.Field("AppointmentType") == "FreeTime"
  select row.Field("TravelTime")
).Min();

I kept your original code with a few modifications to demonstrate the syntax for creating anonymous types.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256