0

I am struggling to figure out how to modify this dictionary so that it can accept two values for one key (if needed) instead of just one value. In most cases there is only one value to a key, but occasionally there are two values, and the application does not like this. Instead it throws "item with the same key already added" exception. I have been researching all over the web about dictionary as I am still learning, but have struggled and thought I might ask here for someone to point me in the right direction. From what I have read I should be using dynamic or a tuple, but I am having trouble implementing that with the code below. I know there are similar questions with answers on this site, but I am struggling to relate these answers to this code. A more specific example is greatly appreciated.

 public List<ImportItem<T>> ProcessReportResult(CSVTable resultData, ICollection<ImportItem<T>> data, Func<T, string> keyFilter)
    {
        WriteLog("{1}{0} records found.{1}", resultData.Rows.Length, Environment.NewLine);

        //key = Order Number; value = Order ID
        var idDictionary = resultData.Rows.Select((row => row.Split(','))).ToDictionary(id => id[0], id => id[1]);

        idDictionary.ForEach(id => WriteLog("Input Id = {0} - Matching record Id = {1}", id.Key, id.Value));

        var processList = data.Where(item => idDictionary.ContainsKey(keyFilter(item.DataItem))).ToList();

        processList.ForEach(item => item.Id = idDictionary[keyFilter(item.DataItem)]);


        return processList;
    }
booksguy
  • 41
  • 6
  • 1
    Use some sort of collection(array/list) for the values – AD.Net Sep 01 '15 at 18:30
  • you can use a class with two property as value for your dictionary.In my opinion don't use tuple because It's immutable. Dictionary dictionary = new Dictionary(); – Arash Sep 01 '15 at 18:38
  • 1
    @booksguy, Use `.ToLookup` instead of `.ToDictionary` – Eser Sep 01 '15 at 18:38
  • thanks for the suggestion Eser, can you elaborate with an example? I am looking at ToLookup now, and I think it should work. Just have to figure out how to implement it. I'm still just a n00b – booksguy Sep 01 '15 at 19:42

2 Answers2

0

Make your value type a collection...

Dictionary<string, IEnumerable<string>>.

You have to do a but more work with the values but you can then store as many alas you like. You can swap in collection types such as hashset if you want performance or uniqueness.

To use this in the context of your comment:

idDictionary.ForEach(id => WriteLog("Input Id = {0} - Matching record Id = {1}", id.Key, string.Concat(id.Value)));

Make your value IEnumerable String, and then use string.Concat to stich them all together. Personally, I'd prefer to use a full notation Foreach loop and build the string using a StringBuilder. It is more verbose, but readable and you add a separator between your values to make it readable.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
  • Thanks for edit, iOS Safari chewed up my code snippet!! – kidshaw Sep 01 '15 at 18:37
  • I like this idea, but could you explain further. How would I manipulate the following line to work with the collection? idDictionary.ForEach(id => WriteLog("Input Id = {0} - Matching record Id = {1}", id.Key, id.Value)); – booksguy Sep 01 '15 at 19:23
-1

Why not just to use IEnumerable<KeyValuePair<T,T>> instead of Dictionary?

unconnected
  • 991
  • 1
  • 10
  • 21