I have a list of customer objects (e.x: List customers)
public class Customer
{
public int ID { get; set; }
public string City { get; set; }
public bool DidLive { get; set; }
}
What I need to do is to convert this “customers” collection into a dictionary like follows,
“Dictionary<int, Dictionary<string, bool>>”
Where the outer key is the “ID” and the inner key is the “City”.
Can this be done using "GroupBy" or "ToDictionary" extension methods of "IEnumerable<T>"?
I’m assuming here that you have multiple Customer objects with the same Id but with different Cities (if this isn’t the case and the inner dictionary will always contain one item, go with @oleksii’s answer).
Of course, this will throw an exception if there multiple customers with identical Ids and Cities.