【发布时间】:2013-07-01 14:28:21
【问题描述】:
我有一个返回以下格式的查询:
{ "tesla", "model s" }
{ "tesla", "roadster" }
{ "honda", "civic" }
{ "honda", "accord" }
我想将其转换为 <string, string[]> 的字典,如下所示:
{ "tesla" : ["model s", "roadster"], "honda" : ["civic", "accord"] }
我试过这个:
var result = query.Select(q => new { q.Manufacturer, q.Car}).Distinct().ToDictionary(q => q.Manufacturer.ToString(), q => q.Car.ToArray());
但到目前为止,我没有任何运气。我认为这实际上是在尝试添加诸如 "tesla" : ["model s"] 和 "tesla" : ["roadster"] 之类的单个项目,这就是它失败的原因......有什么简单的方法可以完成我在 LINQ 中尝试做的事情吗?
【问题讨论】:
标签: c# .net string linq dictionary