简体   繁体   English

C# - 从KeyValuePair列表中删除一个项目

[英]C# - Remove a item from list of KeyValuePair

如何从KeyValuePair列表中删除项目?

If you have both the key and the value you can do the following 如果您同时拥有密钥和值,则可以执行以下操作

public static void Remove<TKey,TValue>(
  this List<KeyValuePair<TKey,TValue>> list,
  TKey key,
  TValue value) {
  return list.Remove(new KeyValuePair<TKey,TValue>(key,value)); 
}

This works because KeyValuePair<TKey,TValue> does not override Equality but is a struct. 这是有效的,因为KeyValuePair<TKey,TValue>不会覆盖Equality但是是一个结构。 This means it uses the default value equality. 这意味着它使用默认值相等。 This simply compares the values of the fields to test for equality. 这只是比较字段的值来测试相等性。 So you simply need to create a new KeyValuePair<TKey,TValue> instance with the same fields. 因此,您只需要创建一个具有相同字段的新KeyValuePair<TKey,TValue>实例。

EDIT 编辑

To respond to a commenter, what value does an extension method provide here? 为了回应评论者,扩展方法在这里提供了什么价值?

Justification is best seen in code. 在代码中最好看到理由。

list.Remove(new KeyValuePair<int,string>(key,value));
list.Remove(key,value);

Also in the case where either the key or value type is an anonymous type, an extension method is required. 此外,在键或值类型是匿名类型的情况下,需要扩展方法。

EDIT2 EDIT2

Here's a sample on how to get KeyValuePair where one of the 2 has an anonymous type. 以下是如何获取KeyValuePair的示例,其中2个中的一个具有匿名类型。

var map = 
  Enumerable.Range(1,10).
  Select(x => new { Id = x, Value = x.ToString() }).
  ToDictionary(x => x.Id);

The variable map is a Dicitonary<TKey,TValue> where TValue is an anonymous type. 变量映射是Dicitonary<TKey,TValue> ,其中TValue是匿名类型。 Enumerating the map will produce a KeyValuePair with the TValue being the same anonymous type. 枚举地图将生成KeyValuePair,其中TValue是相同的匿名类型。

Here are a few examples of removing an item from a list of KeyValuePair: 以下是从KeyValuePair列表中删除项目的几个示例:

// Remove the first occurrence where you have key and value
items.Remove(new KeyValuePair<int, int>(0, 0));

// Remove the first occurrence where you have only the key
items.Remove(items.First(item => item.Key.Equals(0)));

// Remove all occurrences where you have the key
items.RemoveAll(item => item.Key.Equals(0));

EDIT 编辑

// Remove the first occurrence where you have the item
items.Remove(items[0]);

要按键删除列表中的所有项目:

myList.RemoveAll(x => x.Key.Equals(keyToRemove));

应该能够使用.Remove(),. RemoveAt()或其他方法之一。

    List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
    KeyValuePair<string, string> kvp = list[i];
    list.Remove(kvp);

or 要么

    list.Remove(list[1]);

You must obtain a reference to the object you want to remove - that's why I found the item I'm looking for and assigned to a KeyValuePair - since you're telling it to remove a specific item. 您必须获取对要删除的对象的引用 - 这就是为什么我找到了我正在寻找并分配给KeyValuePair的项目 - 因为您告诉它删除特定项目。

A better solution might be to use a dictionary: 更好的解决方案可能是使用字典:

    Dictionary<string, string> d = new Dictionary<string, string>();
    if (d.ContainsKey("somekey")) d.Remove("somekey");

This allows you to remove by the key value instead of having to deal with the list not being indexed by the key. 这允许您通过键值删除,而不必处理未被键索引的列表。

Edit you may not have to get a KeyValuePair reference first. 编辑您可能不必先获得KeyValuePair引用。 Still, a dictionary is probably a better way to go. 不过,字典可能是更好的方式。

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

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