【问题标题】:List of KeyValuePair to be Distinct要区分的 KeyValuePair 列表
【发布时间】:2016-03-03 05:22:19
【问题描述】:

我有一个 KeyValuePair 列表,它的值也是列表,例如

List<KeyValuePair<string, List<string>>> ListX = new List<KeyValuePair<string,List<string>>>();
ListX.Add(new KeyValuePair<string,List<string>>("a",list1));
ListX.Add(new KeyValuePair<string,List<string>>("b",list1));
ListX.Add(new KeyValuePair<string,List<string>>("a",list1));`

我希望列表中每个 KeyValuePair 的键不重复,只有键,我可以在这个列表中使用 Distinct 吗?

例如,我希望删除列表中具有“a”键的第三项,因为它是重复的。

【问题讨论】:

  • 请改用Dictionary&lt;string,List&lt;string&gt;&gt;。如果键已经存在,添加方法会抛出异常。您可以先使用ContainsKey 方法检查密钥是否已存在。或者您可以使用索引器而不是 Add 方法,如果键已经存在,该方法会覆盖旧值。 dic["a"] = list1;

标签: c# asp.net list keyvaluepair


【解决方案1】:

虽然可以使用您当前的List 使其具有Distinct 键,但我认为适合您情况的最简单的解决方案是使用Dictionary&lt;string,List&lt;string&gt;&gt;

正好满足你的需要:

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
dict.Add("a", new List<string>());
dict.Add("b", new List<string>());
dict.Add("a", new List<string>()); //will throw an error

图片:

如果您想在字典中添加&lt;Key,Value&gt; 时需要检查Key 是否已经存在,只需通过ContainsKey 进行检查:

if (dict.ContainsKey(key)) //the key exists

【讨论】:

  • 这很好用,我不知道字典可以有超过 1 个项目。谢谢
【解决方案2】:
var dictionaryX = ListX
    .GroupBy(x => x.Key, (x, ys) => ys.First())
    .ToDictionary(x => x.Key, x => x.Value);

我不确定这是否是您要查找的内容,但它是一个将 ListX 转换为字典的查询,只需为每个重复键获取第一个值。

【讨论】:

    【解决方案3】:

    您可以使用继承自IEnumerable&lt;KeyValuePair&lt;TKey, TValue&gt;&gt; 的类Dictionary&lt;TKey, TValue&gt;。它是 KeyValuePairs 的集合,只允许唯一的键。

    【讨论】:

    • 如果我尝试添加重复键,它会给我一个错误?对吧?我不希望出现错误,我希望它阻止添加它而不给出错误
    • @user1947393 只需在插入项目之前添加一个检查if(!d.ContainsKey(key))
    【解决方案4】:

    你可以用

    Dictionary<TKey, TValue>   
    

    其中 Tkey 和 Tvalue 是通用数据类型。

    例如,它们可以是 int、string、另一个字典等。

    例如Dictionary&lt;int , string&gt;, Dictionary&lt;int , List&lt;employee&gt;&gt;

    在所有这些情况下,键是不同的部分,即不能再次插入相同的键。

    您可以使用 Distinct 检查密钥是否存在,这样即使您尝试添加相同的密钥也不会发生异常

    但是Distinct 只能防止相同的键值对

    防止添加相同的密钥,请使用Enumerable.GroupBy
    ListItems.Select(item => { long value; bool parseSuccess = long.TryParse(item.Key, out value); return new { Key = value, parseSuccess, item.Value }; }) .Where(parsed => parsed.parseSuccess) .GroupBy(o => o.Key) .ToDictionary(e => e.Key, e => e.First().Value)

    【讨论】:

    • 此代码将添加第一个“a”作为键和列表。您不能将相同的键“a”添加到字典中,这是添加字典的唯一目的。
    【解决方案5】:
    List<Dictionary<int, List<int>>> list = new List<Dictionary<int, List<int>>>(); //List with a dictinary that contains a list 
    int key = Convert.ToInt32(Console.ReadLine()); // Key that you want to check if it exist in the dictinary
    int temp_counter = 0; 
    
    foreach(Dictionary<Int32,List<int>> dict in list)
    {
        if(dict.ContainsKey(key))
        temp_counter+=temp_counter;
    }
    
    if (temp_counter == 0) // key not present in dictinary then add a to the list a dictinary object that contains your list
    {
        Dictionary<int,List<int>> a = new Dictionary<int,List<int>>();
        a.Add(key,new List<int>()); // will contain your list
        list.Add(a);
    }
    

    检查这是否有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多