简体   繁体   English

列表中的分区键<keyvaluepair> c#</keyvaluepair>

[英]Partition Keys in List<KeyValuePair> c#

I have a List of KeyValuePairs我有一个 KeyValuePairs 列表

var hitCoord = new List<KeyValuePair<int, double>>()

and sorted like this (descending by Key)并像这样排序(按键降序)

hitCoord.Sort((a, b) => (b.Key.CompareTo(a.Key)));

I can find the total highest Value with我可以找到总的最高价值

hitCoord.Sort((a, b) => (b.Value.CompareTo(a.Value)));

(^ maybe that can be used for the following query?) (^也许可以用于以下查询?)

I would like to partition the Keys in my list such that I can find Values that meet a condition within the specified range of keys.我想对列表中的键进行分区,以便可以在指定的键范围内找到满足条件的值。

ie I would like to find the highest Value and Lowest Value in a range of (int)Keys即我想在(int)键范围内找到最高值和最低值

for (i=0; i<hitCoord.Count; i++)
{
     if (hitCoord[i].Key > (int lowerbound) && hitCoord[i].Key < (int upperBound)
     {
          find highest Value?
     }
}

Not sure if that is at all on the right track.不确定这是否完全在正确的轨道上。 I am new to programming and very new to KeyValuePairs.我是编程新手,对 KeyValuePairs 非常陌生。 Any help you can offer on this matter is much appreciated!非常感谢您在此问题上提供的任何帮助! Thank you!谢谢!

You don't need to actually sort - you can do this with Linq (adding using System.Linq; to the top of your.cs file).您不需要实际排序 - 您可以使用 Linq 执行此操作( using System.Linq;添加到 your.cs 文件的顶部)。 You just want a Where to filter by key and a Max to get the highest value:您只需要一个Where to filter by key 和一个Max以获得最高值:

var maxValue = hitCoord.Where(hc => hc.Key > lowerbound && hc.Key < upperBound)
                       .Max(hc => hc.Value);

Finding the max value in a specified range of keys could be solved by using LINQ ( using System.Linq; ) like this:使用 LINQ ( using System.Linq; )可以解决在指定键范围内查找最大值,如下所示:

hitCoord.Where(c => c.Key > lowerbound && c.Key < upperbound).Max(c => c.Value);

The approach:该方法:

  1. Use Where to filter all items with key in range使用Where过滤范围内键的所有项目
  2. Use Max to get the max value使用Max获取最大值

You could adapt and extend the query also with more checks and constraints.您还可以通过更多检查和约束来调整和扩展查询。 Some basic queries are described in Basic LINQ Query Operations (C#) . 基本 LINQ 查询操作 (C#)中描述了一些基本查询。

As others have suggested this is all pretty easy to do with linq.正如其他人所建议的那样,使用 linq 很容易做到这一点。 here's another sample of linq calls including how to create a partition lookup.这是 linq 调用的另一个示例,包括如何创建分区查找。

var hitCoord = new List<KeyValuePair<int, double>>()
{
    new KeyValuePair<int, double>(1, 1.1),
    new KeyValuePair<int, double>(1, 1.2),
    new KeyValuePair<int, double>(2, 2.0),
    new KeyValuePair<int, double>(2, 2.1)
};

var partitions = hitCoord.ToLookup(kvp => kvp.Key % 2);

var maxKvp = hitCoord.Max(kvp => kvp.Key);
var minKvp = hitCoord.Min(kvp => kvp.Key);

int lower = 1;
int higher = 2;

var maxInRange = hitCoord.Where(kvp => kvp.Key >= lower && kvp.Key <= higher).Max(kvp => kvp.Key);

That said if this is perfromance critical then you'll probably want to use something other than linq so you can optimize it and avoid going through the list multiple times.也就是说,如果这对性能至关重要,那么您可能希望使用 linq 以外的其他东西,以便您可以优化它并避免多次查看列表。

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

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