【发布时间】:2014-08-18 10:14:08
【问题描述】:
我正在尝试通过 Linq 语句创建 Dictionary<string, List<int>>
它给了我以下错误:无法创建类型为“System.Collections.Generic.List`1[System.Int32]”的查询结果
var output = (
from e in EDC.Energideklarationer_As
where MunicipalityName.Contains(e.Municipality)
group e by e.Municipality into g
select new
{
Municipality = g.Key,
listan = new List<int>()
{
g.Count(e => e.H== "El"),
g.Count(e => e.H== "Eldningsolja"),
g.Count(e => e.H== "Flis"),
g.Count(e => e.H== "Markvarmepump"),
g.Count(e => e.H== "Ved")
}
}
).ToDictionary(x => x.Municipality, x=> x.listan);
【问题讨论】:
-
您可以尝试将
select和ToDictionary()分成两行,如果仍然出现错误,请告诉我们?如果是这样,请告诉我们是哪一行导致了错误。 -
感谢您的回复。虽然不确定你的意思。你能给我举个例子吗?
-
var temp = from e in...,然后var output = temp.ToDictionary(x => x.Municipality, x=> x.listan);这样我们就可以知道您的 linq 的哪一部分真正触发了问题。 -
它在 .ToDictionary() 处抱怨同样的错误:无法创建类型为“System.Collections.Generic.List`1[System.Int32]”的查询结果。
-
是
EDC.Energideklarationer_As恰好是IQueryable
标签: c# asp.net-mvc linq dictionary