发现使用LINQ最小和最大日期数组? [英] Find minimal and maximal date in array using LINQ?

查看:486
本文介绍了发现使用LINQ最小和最大日期数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类数组的形式与属性日期,即:

I have an array of classes with a property Date, i.e.:

class Record
{
    public DateTime Date { get; private set; }
}

void Summarize(Record[] arr)
{
    foreach (var r in arr)
    {
        // do stuff 
    }
}

我要找到最早(最小)和最新的(最大)日期在这个数组中。

I have to find the earliest (minimum) and the latest (maximum) dates in this array.

我如何能做到这一点使用LINQ?

How can I do that using LINQ?

推荐答案

如果你想找到的最早或最晚日期:

If you want to find the earliest or latest Date:

DateTime earliest = arr.Min(record => record.Date);
DateTime latest   = arr.Max(record => record.Date);

Enumerable.Min ,的 Enumerable.Max

如果你想找到的最早或最晚日期的记录:

If you want to find the record with the earliest or latest Date:

Record earliest = arr.MinBy(record => record.Date);
Record latest   = arr.MaxBy(record => record.Date);

请参阅:<一href="http://stackoverflow.com/questions/914109/how-to-use-linq-to-select-object-with-minimum-or-maximum-property-value/914198#914198">How使用LINQ选择以最小的或最大的属性值对象

这篇关于发现使用LINQ最小和最大日期数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆