7

I'm trying to use a ConcurrentDictionary to help with a filtering task.

If a number appears in list, then I want to copy an entry from one dictionary to another.

But this part of the AddOrUpdate is not right - v.Add(number)

I get

"Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'

And two more errors.

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        List<int> filter = new List<int> {1,2};
        p.Filter(filter);
    }


    private void Filter(List<int> filter)
    {
        Dictionary<string, List<int>> unfilteredResults = new Dictionary<string, List<int>>();
        unfilteredResults.Add("key1", new List<int> { 1,2,3,4,5});

        ConcurrentDictionary<string, List<int>> filteredResults = new ConcurrentDictionary<string, List<int>>();

        foreach (KeyValuePair<string, List<int>> unfilteredResult in unfilteredResults)
        {
            foreach (int number in unfilteredResult.Value)
            {
                if (filter.Contains(number))
                {
                    filteredResults.AddOrUpdate(unfilteredResult.Key, new List<int> { number }, (k, v) => v.Add(number));
                }
            }
        }
    }
}
Bryan
  • 5,065
  • 10
  • 51
  • 68

1 Answers1

6

Thanks to Lucas Trzesniewski, for pointing out my mistake in the comments - he didn't want to post an answer.

You probably mean: (k, v) => { v.Add(number); return v; }

Bryan
  • 5,065
  • 10
  • 51
  • 68