Prevent Dictionary from being transformed to KeyValuePair when used as IEnumerable.Zip extention parameter

  Kiến thức lập trình

Im having trouble understanding why when I Zip the charDic Dictionary it becomes a KeyValuePair in the resulting serviceDic being a Dictionary<Guid, KeyValuePair<Guid, ICharacteristic>>

The code below works, the charDic is set as a Dictionary<Guid, ICharacteristic> , but then used it results in the KeyValuePair

Dictionary<Guid, ICharacteristic> charDic = [];
foreach (var service in Services)
{
    var characteristics = await service.GetCharacteristicsAsync();
    charDic = characteristics.Select(static characteristic => characteristic.Id).Zip(characteristics).ToDictionary();
}

var serviceDic = Services.Select(static service => service.Id).Zip(charDic).ToDictionary();

Rather I would like the resulting type of serviceDic to be Dictionary<Guid, Dictionary<Guid, ICharacteristic>>

fwiw attempting to ‘keep’ charDic a Dictionary doesnt change anything:

var serviceDic = Services.Select(static service => service.Id).Zip(charDic.ToDictionary()).ToDictionary();

LEAVE A COMMENT