C# 使用 LINQ 在数组中查找最小和最大日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2132615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find minimal and maximal date in array using LINQ?
提问by abatishchev
I have an array of classes with a property Date
, i.e.:
我有一组带有属性的类Date
,即:
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.
我必须在这个数组中找到earliest
(最小)和latest
(最大)日期。
How can I do that using LINQ?
我如何使用 LINQ 做到这一点?
采纳答案by dtb
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
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);
See: How to use LINQ to select object with minimum or maximum property value
回答by Natrium
old school solution without LINQ:
没有 LINQ 的老式解决方案:
DateTime minDate = DateTime.MaxValue;
DateTime maxDate = DateTime.MinValue;
foreach (var r in arr)
{
if (minDate > r.Date)
{
minDate = r.Date;
}
if (maxDate < r.Date)
{
maxDate = r.Date;
}
}
回答by Justin
Using lambda expressions:
使用 lambda 表达式:
void Summarise(Record[] arr)
{
if (!(arr == null || arr.Length == 0))
{
List<Record> recordList = new List<Record>(arr);
recordList.Sort((x,y) => { return x.Date.CompareTo(y.Date); });
// I may have this the wrong way round, but you get the idea.
DateTime earliest = recordList[0];
DateTime latest = recordList[recordList.Count];
}
}
Essentially:
本质上:
- Sort into a new list in order of date
- Select the first and last elements of that list
- 按日期顺序排序为新列表
- 选择该列表的第一个和最后一个元素
UPDATE:Thinking about it, I'm not sure that this is the way to do it if you care at all about performance, as sorting the entire list will result in many more comparisons than just scanning for the highest / lowest values.
更新:考虑一下,如果您完全关心性能,我不确定这是这样做的方法,因为对整个列表进行排序将导致更多的比较,而不仅仅是扫描最高/最低值。
回答by Ravenheart
I'd just make two properties Min,Max, assign them the value of the first item you add to the array, then each time you add a new item just check if its DateTime is less or greater than the Min Max ones.
我只会创建两个属性 Min,Max,将您添加到数组的第一个项目的值分配给它们,然后每次添加新项目时,只需检查其 DateTime 是小于还是大于 Min Max 的。
Its nice and fast and it will be much faster than iterating through the array each time you need to get Min Max.
它既好又快,而且比每次需要获取 Min Max 时遍历数组要快得多。
回答by Johnny Blaze
The two in one LINQ query (and one traversal):
两个合一的 LINQ 查询(和一个遍历):
arr.Aggregate(
new { MinDate = DateTime.MaxValue,
MaxDate = DateTime.MinValue },
(accDates, record) =>
new { MinDate = record.Date < accDates.MinDate
? record.Date
: accDates.MinDate,
MaxDate = accDates.MaxDate < record.Date
? record.Date
: accDates.MaxDate });