根據最高日期從 IGrouping 中獲取項目 (Get an item from a IGrouping based on the highest date)


問題描述

根據最高日期從 IGrouping 中獲取項目 (Get an item from a IGrouping based on the highest date)

In a IEnumerable<Appointment> that contains appointments defined as:

class Appointment
{
  public string Name { get; set; }
  public DateTime LastModified { get; set; }
  public bool IsCancelled { get; set; }
}

which in practice can be like:

"Jon S" | 25-02-2011 16:14:40 | true
"Jon S" | 25-04-2011 22:15:44 | false
"Marc G" | 15-11-2011 16:09:00 | true
"Marc G" | 21-12-2011 16:11:00 | false
"Marc G" | 20-12-2011 16:24:00 | true
"Eric L" | 13-06-2011 19:10:00 | false
"Reed C" | 13-04-2011 09:10:00 | true
"Reed C" | 13-06-2011 19:10:00 | false
"John G" | 04-01-2011 06:12:00 | true
"Jon S" | 25-03-2011 12:24:42 | true

This collection has to be downsized to a collection that contains only the appointments with the highest date LastModified of each distinct Name.

"Jon S" | 25-04-2011 22:15:44 | false
"Marc G" | 21-12-2011 16:11:00 | false
"Eric L" | 13-06-2011 19:10:00 | false
"Reed C" | 13-06-2011 19:10:00 | false
"John G" | 04-01-2011 06:12:00 | true

I am a bit confused how to do this. When I tried:

 .GroupBy(n => n.Name)
 .Select(d => d.Max(t => t.LastModified));

It seems to deliver the correct dates, but the whole entity is needed, what am I missing?. 

UPDATE/note: This is a large collection with about 10.000 unsorted items, delivered by a service I cannot modify. Each distinct group has about 20 elements.


參考解法

方法 1:

.GroupBy(n => n.Name)
.Select(d => d.OrderByDescending(t => t.LastModified).First())

方法 2:

.GroupBy(appt => appt.Name, (key, group) =>
{
    var maxDt = group.Max(appt => appt.LastModified);
    return group.First(appt => appt.LastModified == maxDt);
});

Possibly better performance than previous solution

方法 3:

In Linq query syntax:

        var last = from app in apps
                   orderby app.LastModified descending
                   group app by app.Name into grp
                   select grp.First();                    

Your version with max:

        var lastApp = from app in apps
                      group app by app.Name into grp
                      select grp.First(last => last.LastModified == grp.Max( g => g.LastModified));  

(by Caspar KleijneBFreea553Marino Šimić)

參考文件

  1. Get an item from a IGrouping based on the highest date (CC BY-SA 3.0/4.0)

#lambda #linq #C#






相關問題

Lambda 表達式中的 SQL WHERE 等價物是什麼? (What's the equivalence of an SQL WHERE in Lambda expressions?)

如何將 lambda 傳遞給 Razor 輔助方法? (How to pass in a lambda to a Razor helper method?)

lỗi biểu thức lambda: biểu thức phải là giá trị có thể sửa đổi (lambda expression error: expression must be a modifiable lvalue)

如何在 PySpark 中有效地按值排序? (How to sort by value efficiently in PySpark?)

將列表列表減少為字典,以子列表大小為鍵,出現次數為值 (Reduce list of list to dictionary with sublist size as keys and number of occurances as value)

匿名類作為泛型參數 (anonymous class as generic parameter)

如何為 lambda 中的運算符賦予不定性? (How to give infixities to operators in lambda?)

如何發出委託或 lambda 表達式 (Howto emit a delegate or lambda expression)

深入學習 C# 表達式樹的最佳資源是什麼? (What is the best resource for learning C# expression trees in depth?)

根據最高日期從 IGrouping 中獲取項目 (Get an item from a IGrouping based on the highest date)

如何在 C# 中使用“param”來獲取這個示例? (How can I get this example using "param" in C# to work?)

如何使用 C# 中的 Lambda 表達式僅返回沒有來自另一個表的行的行 (How to return only rows where no rows from another table using Lambda Expressions in C#)







留言討論