将字典转换为自定义KeyValuePair类型列表的最佳方法是什么?

问题描述 投票:-1回答:1

我有课:

public class MyKeyValuePair
{
    public string Key;
    public string Value;

    ...
}

我想知道是否有比这更好的方法:

MyKeyValuePair myList;
Dictionary<string,string> dict;

foreach(var pair in dict)
{
    myList.add(new MyKeyValuePair(pair.Key, pair.Value))
}
c# collections
1个回答
0
投票

Linq方法

var result = dict.Select(x => new MyKeyValuePair() { Key = x.Key, Value = x.Value }).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.