Apply Group by to dictionary value

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());

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật