我正在将我的图表应用程序从预设数据转换为使用数据库。将Linq查询返回到KeyValuePair的列表中
以前我用的是这样的:
var data = new Dictionary<string, double>();
switch (graphselected)
{
case "1":
data = new Dictionary<string, double>
{
{"Dave", 10.023f},
{"James", 20.020f},
{"Neil", 19.203f},
{"Andrew", 4.039f},
{"Steve", 5.343f}
};
break;
case "2":
data = new Dictionary<string, double>
{
{"Dog", 10.023f},
{"Cat", 20.020f},
{"Owl", 19.203f},
{"Rat", 16.039f},
{"Bat", 27.343f}
};
break;
//etc...
}
// Add each item in data in a foreach loop
foreach (var item in list)
{
// Adjust the Chart Series values used for X + Y
seriesDetail.Points.AddXY(item.Key, item.Value);
}
这就是我想要做的事:
var list = new List<KeyValuePair<string, double>>();
switch (graphselected)
{
case "1":
var query = (from x in db2.cd_CleardownCore
where x.TimeTaken >= 10.0
select new { x.IMEI, x.TimeTaken }).ToList();
list = query;
break;
//...
}
我的代码错误对:
list = query;
,出现错误:
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'
to 'System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,double>>'
如何实现转换?
是否'新{x.IMEI,x.TimeTaken}'看起来像一个KeyValuePair给你? – 2014-08-28 16:33:18
来自另一篇文章:“LINQ需要一个无参数的构造函数,因为它想使用集合初始化({}括号)。你可以有额外的类构造函数,但是LINQ不会使用它们。据我了解,这是为什么它的格式与我的代码一样。来自:http://stackoverflow.com/questions/15382840/only-parameterless-constructors-and-initializers-are-supported-in-linq-to-entiti – Baggerz 2014-08-29 08:53:28