views:

32

answers:

1

hi all,

I am trying to get the longest and shortest timespan in a list using LINQ. My code looks something like this

List listofTimeSpans = new List();

adding the timespans to listofTimeSpans in a foreach loop.

Please help me with this. Thank you.

+1  A: 

I would create the list as

var listofTimeSpans = new List<TimeSpan>();

then

var min = listofTimeSpans.Min();
var max = listofTimeSpans.Max();

Regards.

uvita
Thanks for your reply. I was looking at it and there is OrderBy and OrderByDescending functions. Do you know how to order the timespan with total seconds? Thank you for you reply in advance.
shanthiram
You could either use listofTimeSpans.Sort() or var ordered = listofTimeSpans.OrderBy(t=>t);Both have the same results. I didn't understand what you mean by ordering with total seconds, but you can also use var ordered = listofTimeSpans.OrderBy(t=>t.Seconds);
uvita
I am new to LINQ and was thinking wrong. Max and Min functions worked for me.Thank you again.
shanthiram