gpt4 book ai didi

c# - 获取 IGrouping 的第一项

转载 作者:行者123 更新时间:2023-12-02 14:47:44 26 4
gpt4 key购买 nike

IEnumerable<IGrouping<long, MyClass>> datas = list.GroupBy(x => x.PropertyXYOfMyClass);

// get all items from each group
foreach (var grouping in datas)
{
long groupKey = groupingByMyClass.Key;

//iterating through values
foreach (var item in groupingByMyClass)
{
long key = item.PropertyIntOfClassA;
string property = item.PropertyA;
}
}

每个组都包含一些项目,哇,要从每个组的第一个项目中获取值?

更新

void Extract()
{
List<DataHolder> data = new List<DataHolder>();
List<DateTime> randomTimes = new List<DateTime>();
Random r = new Random();
DateTime d = new DateTime(2019, 9, 19, 7, 0, 0);

for (int i = 0; i < 100; i++)
{
DataHolder dh = new DataHolder();
TimeSpan t = TimeSpan.FromSeconds(r.Next(0, 14400));
dh.OID = i;
dh.Value = r.Next(50);
dh.Snapshottime = d.Add(t);

data.Add(dh);
}

data.OrderBy(o => o.Snapshottime).ToList();

List<DataHolder> SortedList = data.OrderBy(o => o.Snapshottime).ToList();

TimeSpan interval = new TimeSpan(0, 15, 0);
var result = SortedList.GroupBy(x => x.Snapshottime.Ticks / interval.Ticks) .OrderBy(x => x.Key);
}

public class DataHolder
{
public int OID { get; set; }
public double Value { get; set; }
public DateTime Snapshottime { get; set; }
}

根据结果,我需要从每组中取出第一项。

最佳答案

试试这个:

var finalResult = result.Select(gpr=>grp.First());

或者如果您想要最早的/最新的/等等,您可以先订购:

var finalResult = result.Select(gpr=>grp.OrderBy(x=>x.SnapShotTime).First());

关于c# - 获取 IGrouping 的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58010191/

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