gpt4 book ai didi

c# - 从具有 Distinct/GroupBy 的 IEnumerable 中选择并排序——可能吗?

转载 作者:可可西里 更新时间:2023-11-01 08:38:23 24 4
gpt4 key购买 nike

假设你有这个:

class LogEntry
{
int ID;
int UserName;
datetime TimeStamp;
string Details;
}

并且您已经提取了一组这样的数据:

ID  Username   Timestamp   Details
1 foo 1/01/2010 Account created
2 zip 2/02/2010 Account created
3 bar 2/02/2010 Account created
4 sandwich 3/03/2010 Account created
5 bar 5/05/2010 Stole food
6 foo 5/05/2010 Can't find food
7 sandwich 8/08/2010 Donated food
8 sandwich 9/09/2010 Ate more food
9 foo 9/09/2010 Ate food
10 bar 11/11/2010 Can't find food

我想要做的是只为每个用户(即 GroupBy 用户名)选择最后一条记录(即按时间戳降序排序)。我可以理解 Distinct 和 GroupBy,但是将它们组合在一个语句中,该语句还返回非不同/分组的字段/属性并按时间戳排序,这让我很头疼。

上面的例子应该得出的结果是:

ID  Username   Timestamp   Details
2 zip 2/02/2010 Account created
8 sandwich 9/09/2010 Ate more food
9 foo 9/09/2010 Ate food
10 bar 11/11/2010 Can't find food

当我确信可以在单个 LINQ 语句中完成时,我不想“作弊”并诉诸冗长的方法。

最佳答案

希望我的 Linq-fu 是正确的:=)

var results = sourceList
.OrderByDescending(item => item.Timestamp)
.GroupBy(item => item.Username)
.Select(grp => grp.First())
.ToArray();

此示例代码使用您的数据,并按 ID 最终排序,提供与您的示例完全相同的输出:(如果您不介意粗略的格式!)

class Program
{
static void Main(string[] args)
{
var sourceItems = new[] {
new LogEntry {ID=1 ,UserName="foo ", TimeStamp= new DateTime(2010 ,1,01),Details="Account created ",} ,
new LogEntry {ID=2 ,UserName="zip ", TimeStamp= new DateTime(2010 ,2,02),Details="Account created ",} ,
new LogEntry {ID=3 ,UserName="bar ", TimeStamp= new DateTime(2010 ,2,02),Details="Account created ",} ,
new LogEntry {ID=4 ,UserName="sandwich ", TimeStamp= new DateTime(2010 ,3,03),Details="Account created ",} ,
new LogEntry {ID=5 ,UserName="bar ", TimeStamp= new DateTime(2010 ,5,05),Details="Stole food ",} ,
new LogEntry {ID=6 ,UserName="foo ", TimeStamp= new DateTime(2010 ,5,05),Details="Can't find food ",} ,
new LogEntry {ID=7 ,UserName="sandwich ", TimeStamp= new DateTime(2010 ,8,08),Details="Donated food ",} ,
new LogEntry {ID=8 ,UserName="sandwich ", TimeStamp= new DateTime(2010 ,9,09),Details="Ate more food ",} ,
new LogEntry {ID=9 ,UserName="foo ", TimeStamp= new DateTime(2010 ,9,09),Details="Ate food ",} ,
new LogEntry {ID=10 ,UserName="bar ", TimeStamp= new DateTime(2010,11,11),Details="Can't find food ",} ,
};

var results = sourceItems
.OrderByDescending(item => item.TimeStamp)
.GroupBy(item => item.UserName)
.Select(grp => grp.First())
.OrderBy(item=> item.ID)
.ToArray();

foreach (var item in results)
{
Console.WriteLine("{0} {1} {2} {3}",
item.ID, item.UserName, item.TimeStamp, item.Details);
}
Console.ReadKey();
}
}


public class LogEntry
{
public int ID;
public string UserName;
public DateTime TimeStamp;
public string Details;
}

关于c# - 从具有 Distinct/GroupBy 的 IEnumerable 中选择并排序——可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3965464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com