1
    public static IEnumerable<KeyValuePair<string, string>> GetGroupKeyValuePairs(string category)
    {
        var list = new List<KeyValuePair<string, string>>();

        using (DataConnection connection = new DataConnection())
        {
            List<KeyValuePair<string,string>> settings = connection.Get<Settings>()
                .Where(a => a.Category == category )
                .Select(pair => new KeyValuePair<string,string>(pair.TheName, pair.TheValue))
                .ToList();

            list = settings;

        }

        return list;
    }

The exception is:

InvalidOperationException: Key 'Garanti.Oda' appears more than one time

How can I collect duplicate keys?

Nime Cloud
  • 6,162
  • 14
  • 43
  • 75
  • 6
    @mybirthname: The OP should be able to create as many `KeyValuePair` instances with the same key as they like, and they should be able to add as many of them to a simple `List` as they like. There is nothing inherent to that structure that would prevent the same key from existing in several `KeyValuePair` structures at a time. – O. R. Mapper Dec 01 '14 at 21:06
  • @O.R.Mapper I will read about the Key,Value pairs to check what your wrote, I used them 2-3 times probably my mistake. Thanks ! – mybirthname Dec 01 '14 at 21:09
  • 1
    @mybirthname: https://dotnetfiddle.net/SElgkq – Thomas Weller Dec 01 '14 at 21:10
  • @OrelEraki: How do you get the idea that they "can't"? Also, what `NameValuePair` type are you referring to? The one from [`Microsoft.Build.Framework.XamlTypes`](http://msdn.microsoft.com/en-us/library/microsoft.build.framework.xamltypes.namevaluepair%28v=vs.110%29.aspx)? Or the one from [`Microsoft.Rtc.Signaling`](http://msdn.microsoft.com/en-us/library/microsoft.build.framework.xamltypes.namevaluepair%28v=vs.110%29.aspx)? None of those sounds like they should or could be used from 3rd party general-purpose code. – O. R. Mapper Dec 01 '14 at 21:14
  • @ThomasW. my bad sorry for stupid comment I delete it ! – mybirthname Dec 01 '14 at 21:15
  • 1
    @O.R.Mapper Absolutely right.. – Orel Eraki Dec 01 '14 at 21:16
  • Where is the exception exactly thrown? Looks like most of us suspect the `KeyValuePair` etc. (and to be honest, me as well), but could it also come from the database? Also databases use a `Key`? If `connection.Get()` throws the exception, the problem could be the database. – Sjips Dec 01 '14 at 21:50

2 Answers2

2

The method that you show isn't going to have a problem with multiple pairs with the same key. I assume that afterward, you're doing something like creating a dictionary of these pairs, and that's where you have a problem. E.g.

var pairs = GetGroupKeyValuePairs("some category");
var dict = new Dictionary<string, string>();
foreach (var pair in pairs)
    dict.Add(pair.Key, pair.Value); // exception when it hits a duplicate

Instead, you need to use the pairs in a way that's friendly to duplicates, e.g. ToLookup.

var pairs = GetGroupKeyValuePairs("some category");
var lookup = pairs.ToLookup(x => x.Key, x => x.Value);

Then, for example if the list had "a", "b" and "a", "c", then lookup["a"] gives you "b" and "c".

Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

Assuming you want to find duplicates by Key only (e.g. so that you can build a dictionary), you could GroupBy the prospective key and find all instances of more than one:

 var dupeSettings = connection.Get<Settings>()
            .Where(a => a.Category == category)
            .GroupBy(a => a.TheName)
            .Where(grp => grp.Count() > 1)
            .Select(dupe => dupe.Key)
            .ToList();

Or, if you want duplicates of both key and value, project and group by an anonymous class:

 var dupeSettings = connection.Get<Settings>()
            .Where(a => a.Category == category)
            .GroupBy(a => new {a.TheName, a.TheValue})
            .Where(grp => grp.Count() > 1)
            .Select(dupe => dupe.Key) // Key.TheName, Key.TheValue
            .ToList();
StuartLC
  • 104,537
  • 17
  • 209
  • 285