首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将List<T>转换为Dictionary<string,并将密钥设置为属性T的T>

将List<T>转换为Dictionary<string,并将密钥设置为属性T的T>
EN

Stack Overflow用户
提问于 2019-04-08 07:58:42
回答 1查看 661关注 0票数 0
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

最好的转换方式是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-08 08:04:48

如果我理解你在问什么(这是一个很大的假设),它应该像这样简单

var someDictionary = quotes.ToDictionary(x => x.Symbol);

Enumerable.ToDictionary Method

从IEnumerable创建字典。

还可以看一看

Enumerable.ToLookup Method

从IEnumerable创建通用查找。

字典是1:1映射(每个键映射到单个值),并且字典在事件发生后是可变的(可编辑的)。

查找是1:多映射(多映射;每个键都映射到具有该键的值的一个IEnumerable<> ),并且在ILookup<,>接口上没有任何变化。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55564713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档