简体   繁体   中英

List to Dictionary<Key, List<Value>> - C#

I have a List and MyClass is:

public class MyClass
{
    public bool Selected { get; set; }
    public Guid NoticeID { get; set; }
    public Guid TypeID { get; set; }
}

My question is, how do i convert this list into a Dictionary<Guid, List<Guid>> , where the dictionary key is the GUID from the TypeID property, and the value is a list of all the NoticeID values corresponding to that TypeID . I have tried like so:

list.GroupBy(p => p.TypeID).ToDictionary(p => p.Key, p => p.ToList())

but this returns a Dictionary <Guid, List<MyClass>> , and I want a Dictionary<Guid, List<Guid>> .

Well, when you group you can specify the value you want for each element of the group:

var dictionary = list.GroupBy(p => p.TypeID, p => p.NoticeID)
                     .ToDictionary(p => p.Key, p => p.ToList());

However, I would strongly consider using a lookup instead of a dictionary:

var lookup = list.ToLookup(p => p.TypeID, p => p.NoticeID);

Lookups are much cleaner in general:

  • They're immutable, whereas your approach ends up with lists which can be modified
  • They express in the type system exactly what you're trying to express (one key to multiple values)
  • They make looking keys up easier by returning an empty sequence of values for missing keys, rather than throwing an exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM