public class Quote
{
public decimal Price { get; set; }
public string Symbol { get; set; }
}
public List<Quote> quotes =
new List<Quote>(){new Quote{Price=100.00,Symbol="AAPL"},
new Quote{Price=200.00,Symbol="GOOG"}}
List<string,Quote> positions = new List<string,Quote>();
我希望将每个position.Key
设置为Quote.Symbol
,将每个position.Value
设置为Quote
最好的转换方式是什么?
发布于 2019-04-08 08:04:48
如果我理解你在问什么(这是一个很大的假设),它应该像这样简单
var someDictionary = quotes.ToDictionary(x => x.Symbol);
Enumerable.ToDictionary Method
从IEnumerable创建字典。
还可以看一看
从IEnumerable创建通用查找。
字典是1:1映射(每个键映射到单个值),并且字典在事件发生后是可变的(可编辑的)。
查找是1:多映射(多映射;每个键都映射到具有该键的值的一个IEnumerable<>
),并且在ILookup<,>
接口上没有任何变化。
https://stackoverflow.com/questions/55564713
复制相似问题