Как зациклить словарь? - PullRequest
0 голосов
/ 12 июня 2018

класс:

class myClass
{
    public int processId { get; set; }
    public string measurement { get; set; }
    public decimal measurementValue { get; set; }
    public string otherText { get; set; }
}

Код:

List<myClass> myClasses = new List<myClass> {
    new myClass { processId=1, measurement="height", measurementValue=10,otherText="312312" },
    new myClass { processId=1, measurement="length", measurementValue=11 ,otherText="312312"},
    new myClass { processId=1, measurement="width", measurementValue=12 ,otherText="312312"},
    new myClass { processId=2, measurement="height", measurementValue=20 ,otherText="312312"},
    new myClass { processId=2, measurement="length", measurementValue=21 ,otherText="312312"},
    new myClass { processId=2, measurement="width", measurementValue=22 ,otherText="312312"}
};

var groups = myClasses
    .GroupBy(o => o.processId)
    .ToDictionary(g => g.Select(x => x.measurement), g => g.Select(x => x.measurementValue));

groups is Dictionary<IEnumerable<string>, Dictionary<IEnumerable<decimal>>

Как зациклить группу в группах для всех ключей изначение?Я не смог понять это.

foreach(var group in groups)
{
    //????
}

1 Ответ

0 голосов
/ 12 июня 2018

Результат groups объект не выглядит логичным для меня.Этот словарь облегчает цикл, и он также кажется более логичным:

var groups = myClasses
    .GroupBy(o => o.processId)
    .ToDictionary(g => g.Key, g => g.Select(x => new {Measurement = x.measurement, Value = x.measurementValue}));

Затем цикл и ведение журнала по группам:

foreach (var item in groups)
{
    Debug.WriteLine($"Key: {item.Key}, Value: {"\t" + string.Join(Environment.NewLine + "\t\t\t\t", item.Value.Select(i => $"{nameof(i.Measurement)}:{i.Measurement},{nameof(i.Value)}:{i.Value}"))}");
}

даст результат:

Key: 1, Value:  Measurement:height,Value:10
                Measurement:length,Value:11
                Measurement:width,Value:12 
Key: 2, Value:  Measurement:height,Value:20
                Measurement:length,Value:21
                Measurement:width,Value:22
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...