3

I have a list of KeyValuePair which its values are list too such as

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));`

I want the keys of each KeyValuePair in the list to be not duplicated, only the keys, can I use Distinct in this list?

for example I want the third item in the list that has "a" key to be deleted because it's duplicated.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
user1947393
  • 153
  • 4
  • 18
  • 3
    use `Dictionary>` instead. Add method throws exception if key already exist. you can use `ContainsKey` method first to check if key already exist. or you can use indexer instead of Add method which overwrites the old value if key already exist. `dic["a"] = list1;` – M.kazem Akhgary Mar 03 '16 at 05:26

5 Answers5

3

Though it is possible to work around with your current List to make it having Distinct keys, the simplest solution which I think fit for your case is to use Dictionary<string,List<string>>

It does just exactly what you need:

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

Image:

enter image description here

If you need to check if a Key is already exist when you want to add a <Key,Value> to a your dictionary, simply check by ContainsKey:

if (dict.ContainsKey(key)) //the key exists
Ian
  • 30,182
  • 19
  • 69
  • 107
3
var dictionaryX = ListX
    .GroupBy(x => x.Key, (x, ys) => ys.First())
    .ToDictionary(x => x.Key, x => x.Value);

I'm not sure if this is what you were looking for, but it's a query that will convert a ListX into a dictionary by only taking the first value for each duplicate key.

devuxer
  • 41,681
  • 47
  • 180
  • 292
0

You can use class Dictionary<TKey, TValue> which inherits from IEnumerable<KeyValuePair<TKey, TValue>>. It is a collection of KeyValuePairs which allows only unique keys.

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34
  • It will give me an error if I'm trying to add duplicate key?right? I don't want an error to be appeared, I want it to prevent adding it without giving an error – user1947393 Mar 03 '16 at 05:45
  • @user1947393 Just add a check before inserting an item `if(!d.ContainsKey(key))` – Ivan Gritsenko Mar 03 '16 at 05:55
0

U can use

Dictionary<TKey, TValue>   

where Tkey and Tvalue are generic datatypes.

For example they can be int, string,another dictionary etc.

ExampleDictionary<int , string>, Dictionary<int , List<employee>> etc.

In all these cases the key is the distinct part ie, same key cannot be inserted again.

U can check if key exists using Distinct so that no exception occurs even if u try to add same key

However Distinct prevents only same key value pairs .

To prevent same key being added use 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)

DAre G
  • 177
  • 10
  • This code will add the first "a" as key and the List. You cannot add the same key "a" to the dictionary ,which is the sole purpose of adding a dictionary. – DAre G Mar 03 '16 at 06:00
0
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);
}

Check if this works

vivek verma
  • 1,716
  • 1
  • 17
  • 26