I have a Dictionary which key of string type and value of list of integer type, like this:
Dictionary<string, List<int>>
.
Lets say I add elements to it:
Dictionary<string, List<int>> check2 = new Dictionary<string, List<int>>();
check2.Add("LD010101", new List<int> { 1, 2, 3 });
check2.Add("LD010201", new List<int> { 1 });
check2.Add("LD030101", new List<int> { 2, 3, 4 });
check2.Add("LD030201", new List<int> { 1, 3, 4 });
check2.Add("LD040101", new List<int> { 5, 1, 4 });
check2.Add("LD050101", new List<int> { 1, 3, 4 });
Now I want to apply group by based on the integer value and filter it. Let’s say group by filter is 2. Then the output has to be a dictionary like this Dictionary<int, List<string>>
, so with filter 2, the output will be a dictionary like this, and I have to use GroupBy
to do this:
key:2, Value: {"LD010101","LD030101"}
1
This method is an example, giving the first Dictionary<int, List<string>>
and the filter
as parameters, and returning a new Dictionary<int, List<string>>
that groups the first keys by the filter.
public static Dictionary<int, List<string>> FilterDictionaryByGroup(Dictionary<string, List<int>> inputDict, int filter)
{
var grouped = inputDict
.Where(el => el.Value.Contains(filter))
.GroupBy(el => filter)
.ToDictionary(g => g.Key, g => g.Select(el => el.Key).ToList());
return grouped;
}
var result = FilterDictionary(check2, filter);
Bare in mind that this approach handles only dealing with one filter value (in your case 2
for example), but since you want the return type to be Dictionary<int, List<string>>
, you can still adhere to the same approach by adding List<int> filters
to the method args instead of a single int
, and iterate over them.
Edit
The Where
clause is self-explanatory, it iterates through each element and “filters” them by returning only the elements where their values List contains the filter (here 2 for example, since that’s only what we want).
g.Select(el => el.Key)
means “for each element (el
) in the group (g
), select its key”. This extracts the keys of all the dictionary entries in the group.
ToList()
converts the selected keys into a list. This is necessary since we want the value of dictionary element to be a List<string>
and not the generic IEnumerable<string>
that Select
returns.
1
Use a combination of filtering (e.g. using LINQ’s .Where()
method) and List.Contains()
:
var filter = 2;
var value = check2
.Where(el => el.Value.Contains(filter))
.Select(el => el.Key)
.ToList();
var newDict = new Dictionary<int, List<string>> { { filter, value } };
2
Requirement: given an integer value, give me all keys of the Dictionary that have at least one value equal to this integer value in the list that belongs to this key.
Dictionary<TKey, TValue>
implements ICollection<KeyValuePair<TKey,TValue>>
. This means that you can enumerate over the items in the dictionary as if they are a combination of the Key and the Value.
In your case: the string and the list.
So from every <string, list> pair, you only want to keep those that have at least once the given integer value in the list.
int searchValue = ...
Dictionary<string, List<int>> myDictionary = ...
IEnumerable<string> keysWithAtLeastOneSearchValueInTheList = myDictionary
.Where(keyValuePair => keyValuePair.Value.Contains(searchValue))
.Select(keyValuePair => kayValuePair.Key);
In words: myDictionary is also an enumerable sequence of KeyValuePairs, where the Key is the string, and the value is the List that belongs to this key. From this enumerable sequence, keep only those KeyValuePairs of which property Value (= the list of integers) contains the searchValue. From the remaining KeyValuePairs select the Key (= string)
The simplest solution, IMHO, is this:
ILookup<int, string> lookup =
check2
.SelectMany(x => x.Value, (x, Value) => (x.Key, Value))
.ToLookup(x => x.Value, x => x.Key);
Then you can get the output you want by this:
List<string> output = lookup[2].ToList();
That gives:
LD010101
LD030101
If you must have it as a Dictionary<int, List<string>>
then this works:
Dictionary<int, List<string>> lookup =
check2
.SelectMany(x => x.Value, (x, Value) => (x.Key, Value))
.ToLookup(x => x.Value, x => x.Key)
.ToDictionary(x => x.Key, x => x.ToList());