简体   繁体   English

使用Linq查找C#通用列表中的最新项目

[英]Find the newest item in a C# generic list using Linq

I have a generic list of items. 我有一个通用的项目清单。 Each item contains a DateTime field. 每个项目都包含一个DateTime字段。 I would like to find the newest item in the list using Linq in the most elegant and efficient way. 我想以最优雅和最有效的方式使用Linq找到列表中的最新项目。

Elegance is more important than efficiency in my case, but doing this also in an efficient way would be nice. 在我的情况下,优雅比效率更重要,但是以有效的方式做这件事也会很好。

Thank you. 谢谢。

After reading the answers: Here is the code (and the answer I liked): 阅读答案后:这是代码(和我喜欢的答案):

using System.Collections.Generic;
using System.Linq;

class Item
{
    public Item Date { get; set; }
    public string Name { get; set; }
}

static void Main(string[] args)
{
    List<Item> items = CreateItems();
    Item newest;
    if (items.Count == 0)
        newest = null;
    else
        newest = items.OrderByDescending(item => item.Date).First();
}

For elegance, i would go by sorting the set based on the datetime field and returning the first item like: 为了优雅,我会根据日期时间字段对集合进行排序并返回第一个项目,如:

set.OrderByDescending(x => x.DateTime)
   .FirstOrDefault();

This will create an in-memory representation of the sorted collection so efficiency is not that good. 这将创建已排序集合的内存中表示,因此效率不高。 The most efficient solution for an unsorted set would be to loop over all items and save the newest. 对于未排序的集合,最有效的解决方案是循环所有项目并保存最新的。 You can use linq for this by performing an aggregate operation which i find syntactically a mess. 您可以通过执行聚合操作来使用linq,我发现在语法上一团糟。

Alternatively, you can store your items in a sorted collection like the SortedSet. 或者,您可以将项目存储在SortedSet等已排序的集合中。 This has a bit more complex insertion time 0(log2) instead of O(1) for most collections but it allows you to immidiatly sort on datetime and therefore selecting the newest item in O(1) rather than O(n). 对于大多数集合,这有一个更复杂的插入时间0(log2)而不是O(1),但它允许您对日期时间进行无差别排序,因此选择O(1)中的最新项而不是O(n)。

Most of the solutions so far have to completely sort the list first (via OrderByDescending), which is unnecessary and time consuming. 到目前为止,大多数解决方案必须首先对列表进行完全排序(通过OrderByDescending),这是不必要且耗时的。 What you want is Jon Skeet's MoreLinq MaxBy function. 你想要的是Jon Skeet的MoreLinq MaxBy功能。 Source for MaxBy is on google code . MaxBy的来源是谷歌代码

var newest = thelist.MaxBy(x => x.DateTimeField);

试试看:

var newItem = myList.OrderByDescending(item => item.yourDateTimeField).First();

Try Aggregate, ie something like: 尝试聚合,即:

list.Aggregate (
    DateTime.MinValue,
    (lastOne, current) => current.GreaterThan (lastOne) ? current : lastOne
)

ie if your field is DateTimeField, you should write something like 即如果你的字段是DateTimeField,你应该写类似的东西

list.Aggregate (
    null,
    (lastOne, current) => 
        (lastOne == null) ||
             current.DateTimeField.GreaterThan (lastOne.DateTimeField)
        ? current
        : lastOne
)

试试这个

 sortlist.OrderByDescending(a => a.timeStamp).First();

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

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