[英]C# - Getting the 3 lowest values in a List, measured by a public property of the objects within the List
Sorry if my terminology is not great, I'm not a professional programmer.对不起,如果我的术语不是很好,我不是专业的程序员。
I have a List< Something >, whereby the 'Something' is a struct.我有一个 List<Something>,其中“Something”是一个结构。 This struct contains objects, which each have their own public properties/fields in the classes.
该结构包含对象,每个对象在类中都有自己的公共属性/字段。 I want to sort the list in order - but by the values found in these nested properties/fields.
我想按顺序对列表进行排序 - 但通过在这些嵌套属性/字段中找到的值。 Then I want to return a list of these values, not the structs.
然后我想返回这些值的列表,而不是结构。
I know this is very confusing but I've had trouble trying to do it.我知道这很令人困惑,但我在尝试时遇到了麻烦。 At the moment I just get a list returned with a count of 20 (which is the full data set I'm using), but I want the 3 values only with the smallest value.
目前我只得到一个返回的列表,计数为 20(这是我正在使用的完整数据集),但我只想要 3 个值的最小值。
For context and further explanation, here is some code I'm using:对于上下文和进一步解释,这是我正在使用的一些代码:
// Returns 3 nearest stations to the location specified
public static List<TrainStation> nearbyStations(GeoCoordinate location)
{
List<StationWalk> stations = new List<StationWalk>();
foreach (TrainStation s in All)
{
stations.Add(new StationWalk(s, new Walk(location, s.location)));
}
// return 3 TrainStation objects that have the lowest StationWalk.Walk.duration values corresponding with them in the StationWalk struct
stations.OrderBy(walks => walks.walk.duration).Take(3);
List<TrainStation> returnList = new List<TrainStation>();
foreach (StationWalk s in stations)
{
returnList.Add(s.station);
}
return returnList;
}
private struct StationWalk
{
public StationWalk(TrainStation station, Walk walk)
{
this.station = station;
this.walk = walk;
}
public TrainStation station;
public Walk walk;
}
'Walk' is a class that contains a 'duration' field. “Walk”是一个包含“duration”字段的类。 This represents the time it takes to walk.
这代表步行所需的时间。 More specifically, my overall goal here is to figure out which 3 walks are the fastest walks out of all 20 in the list.
更具体地说,我在这里的总体目标是找出列表中所有 20 次步行中,哪 3 次步行最快。 But the 'walks' are properties of the StationWalk struct, and the 'duration' is a property of the Walk.
但是“walks”是 StationWalk 结构的属性,而“duration”是 Walk 的属性。
How would I go about doing this?我该怎么做呢? Really sorry if this isn't well explained, it's confusing to myself despite writing it myself, yet alone trying to explain it to others.Appreciate any help.
真的很抱歉,如果这没有得到很好的解释,尽管是自己写的,但我自己却很困惑,但独自试图向其他人解释。感谢任何帮助。
The OrderBy
and Take
both return a new collection, they do not modify the existing collection, so you would need to store the reference to new collection returned by the methods like: OrderBy
和Take
都返回一个新集合,它们不会修改现有集合,因此您需要存储对由以下方法返回的新集合的引用:
stations = stations.OrderBy(walks => walks.walk.duration).Take(3).ToList();
and if you want to keep reference to the original list for further usage down in your code, then just store the result in a local variable:如果您想在代码中保留对原始列表的引用以供进一步使用,则只需将结果存储在局部变量中:
var lowestThreeStations = stations.OrderBy(walks => walks.walk.duration).Take(3).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.