C# – how do i get the min from a linq to dataset query

asp.net-3.5c++linq

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

Best Solution

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.