我正在将我的图表应用程序从预设数据转换为使用数据库。
以前我使用的是这个:
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>>'
我该如何实现转化?
new { x.IMEI, x.TimeTaken }
是否看起来像一个键值对? - Jeff Mercado