简体   繁体   English

使用linq从日期列表计算平均年龄

[英]Calculate Average Age from a list of dates using linq

I have a list of dates where I want to calculate the average age. 我有要计算平均年龄的日期列表。 I'm not sure what the best way of doing this using LINQ. 我不确定使用LINQ的最佳方法是什么。

2017-04-13 08:31:00.000
2017-04-12 07:53:00.000
2017-04-11 07:59:00.000
2017-04-10 08:16:00.000
2017-04-09 15:11:00.000
2017-04-08 08:28:00.000
2017-04-06 08:26:00.000

Should I convert the date values to ticks and then calculate the average? 我应该将日期值转换为刻度,然后计算平均值吗?

Should I convert the date values to ticks and then calculate the average? 我应该将日期值转换为刻度,然后计算平均值吗?

No need - you can subtract two DateTimes and get a TimeSpan that will give you the difference between them in whatever unit you want. 不需要-您可以减去两个DateTimes并获得一个TimeSpan ,这将为您提供所需的任何单位之间的差异。 Just get the number of days (including fractions) between now (or whatever reference date is appropriate) and each item and average them: 只需获取从现在(或任何合适的参考日期)到每一项之间的天数(包括分数),然后将它们平均即可:

double avgDays   = list.Average(dt => (DateTime.Now - dt).TotalDays);

You can use LINQ's Average function. 您可以使用LINQ的平均功能。

IEnumerable<DateTime> timeSpan = new List<DateTime>()
{
    DateTime.Parse("2017-04-13 08:31:00.000"),
    DateTime.Parse("2017-04-12 07:53:00.000"),
    DateTime.Parse("2017-04-11 07:59:00.000"),
    DateTime.Parse("2017-04-10 08:16:00.000"),
    DateTime.Parse("2017-04-09 15:11:00.000")
};

DateTime average = new DateTime((long)timeSpan.Average(x => x.Ticks));

DateTime , internally, is represented by a specific number of ticks, that can be retrieved using the Ticks property. DateTime内部, DateTime由特定的刻度数表示,可以使用Ticks属性检索。 Since Enumerable.Average() does not work with DateTime and Timestamp types, you could proceed as follows: 由于Enumerable.Average()不适用于DateTimeTimestamp类型,因此可以按以下步骤进行操作:

List<DateTime> dates = new List<DateTime>()
{
    DateTime.ParseExact("2017-04-13 08:31:00.000", "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture),
    DateTime.ParseExact("2017-04-12 07:53:00.000", "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture),
    DateTime.ParseExact("2017-04-11 07:59:00.000", "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture),
    DateTime.ParseExact("2017-04-10 08:16:00.000", "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture),
    DateTime.ParseExact("2017-04-09 15:11:00.000", "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)
};

Int64 avgTicks = (Int64)dates.Select(d => d.Ticks).Average();
DateTime avgDate = new DateTime(avgTicks);

If by "age" you mean years and you are looking for a diff to the respect of the current year, then: 如果用“年龄”表示年份,并且您希望与本年度的区别,那么:

Int32 avgYear = DateTime.Now.Year - avgDate.Year;

If you want an absolute difference: 如果您希望绝对不同:

Int32 avgYear = Math.Abs(DateTime.Now.Year - avgDate.Year);

Using an extension method, 使用扩展方法,

public static TimeSpan Average(this IEnumerable<TimeSpan> spans) => TimeSpan.FromSeconds(spans.Select(s => s.TotalSeconds).Average());

You can compute the ages as TimeSpan s and get the average TimeSpan: 您可以将年龄计算为TimeSpan并获得平均TimeSpan:

var now = DateTime.Now; // don't slide the ages by computational time
var avgAge = birthDates.Select(d => now - d).Average();

Or expand the extension method manually: 或手动扩展扩展方法:

var avgAge = TimeSpan.FromSeconds(birthDates.Select(d => (now - d).TotalSeconds).Average());

Then you can use convert the avgAge to whatever units you need: 然后,您可以使用将avgAge转换为avgAge的任何单位:

var avgAgeInYears = (avgAge.TotalDays/365.2425);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM