C# 选择 KeyValuePair 列表的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11390561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:41:59  来源:igfitidea点击:

Select Value of List of KeyValuePair

c#

提问by Devi

How can i select the value from the List of keyvaluepair based on checking the key value

如何基于检查键值从键值对列表中选择值

List<KeyValuePair<int, List<Properties>> myList = new List<KeyValuePair<int, List<Properties>>();

Here I want to get the

在这里我想得到

list myList[2].Value when myLisy[2].Key=5.

How can i achieve this?

我怎样才能做到这一点?

采纳答案by pangabiMC

If you need to use the List anyway I'd use LINQfor this query:

如果您无论如何都需要使用列表,我将使用LINQ进行此查询:

var matches = from val in myList where val.Key == 5 select val.Value;
foreach (var match in matches)
{
    foreach (Property prop in match)
    {
        // do stuff
    }
}

You may want to check the matchfor null.

您可能想要检查匹配是否为空。

回答by Thorsten Dittmar

Use Dictionary<int, List<Properties>>. Then you can do

使用Dictionary<int, List<Properties>>. 然后你可以做

List<Properties> list = dict[5];

As in:

如:

Dictionary<int, List<Properties>> dict = new Dictionary<int, List<Properties>>();
dict[0] = ...;
dict[1] = ...;
dict[5] = ...;

List<Properties> item5 = dict[5]; // This works if dict contains a key 5.
List<Properties> item6 = null;

// You might want to check whether the key is actually in the dictionary. Otherwise
// you might get an exception
if (dict.ContainsKey(6))
    item6 = dict[6];

回答by Pranay Rana

NOTE

笔记

The generic Dictionary class, introduced in .NET 2.0, uses KeyValuePair.

.NET 2.0 中引入的通用 Dictionary 类使用 KeyValuePair。

ITs better you make use of

你可以更好地利用它

Dictionary<TKey, TValue>.ICollection<KeyValuePair<TKey, TValue>>

and use ContainsKey Methodto check the the key is there or not ..

并用于ContainsKey Method检查密钥是否存在..

Example :

例子 :

ICollection<KeyValuePair<String, String>> openWith =
            new Dictionary<String, String>();
openWith.Add(new KeyValuePair<String,String>("txt", "notepad.exe"));
openWith.Add(new KeyValuePair<String,String>("bmp", "paint.exe"));
openWith.Add(new KeyValuePair<String,String>("dib", "paint.exe"));
openWith.Add(new KeyValuePair<String,String>("rtf", "wordpad.exe"));

if (!openWith.ContainsKey("txt"))
{
       Console.WriteLine("Contains Given key");
}


EDIT

编辑

To get value

获取价值

string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
    //in you case 
   //var list= dict.Values.ToList<Property>(); 
}

in your caseu it will be

在你的情况下,它将是

var list= dict.Values.ToList<Property>(); 

回答by Daniel Sklenitzka

If you're stuck with the List, you can use

如果您坚持使用列表,则可以使用

myList.First(kvp => kvp.Key == 5).Value

Or if you want to use a dictionary (which might suit your needs better than the list as stated in the other answers) you convert your list to a dictionary easily:

或者,如果您想使用字典(它可能比其他答案中所述的列表更适合您的需求),您可以轻松地将列表转换为字典:

var dictionary = myList.ToDictionary(kvp => kvp.Key);
var value = dictionary[5].Value;