I have an Item
class
public class Item {
//...
public DateTime CreateDate { get; set; }
public DateTime EndDate { get; set; }
}
In a method, I have List<Item> items
. How would I query average of date differences in days more succinctly than looping through the list?
double days = 0;
int total = 0;
foreach (var @t in results)
{
if (@t.EndDate != null)
{
total++;
TimeSpan ts = (TimeSpan)(@t.EndDate - @t.CreateDate);
days = ts.TotalDays;
}
}
double avg = days / total;