ConcurrentDictionary AddOrUpdate List

I am trying to use ConcurrentDictionary to help with the filtering task.

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

But this part of AddOrUpdate is wrong - v.Add(number)

I get

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

And two more mistakes.

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));
                }
            }
        }
    }
}
+4
source share
1 answer

Thanks to Lucas Trzesniewski for pointing out my mistake in the comments - he did not want to post the answer.

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

+3
source

Source: https://habr.com/ru/post/1627067/


All Articles