Let's say I've a class Telemetry as follows:
public class Telemetry
{
public string Id { get; set; }
public AdditionalInfo Info { get; set; }
}
Let's say I've a class: TelemetryCollection that implements : IEnumerable<IList<Telemetry>>
I need to group an instance of TelemetryCollection based on Telemetry.Id and get a mapping of Telemetry.Id and TelemetryCollection
How do I get an IDictionary<string, IEnumerable<IList<Telemetry>>> from IEnumerable<IList<Telemetry>>>
Assuming
TelemetryCollectioncontains arrays/lists ofTelemetrymeasurements for the sameIdwith possible duplicates, you can:IdToDictionaryto collect the grouped arrays into a dictionaryThe following example compiles:
And prints:
Serializing
dictto JSON produces:This won't work if there are any empty arrays in
TelemetryCollection. This can be fixed ifFirstOrDefault()is used to retrieve the first key and defaulting to""if there's none :