我正在尝试将以下查询返回到字符串数组:
select top 10 c_initials, MAX(c_score) as MaxScore
from t_bisGame
group by c_initials order by MaxScore desc
[WebMethod]
public string[] GetMyGameList()
{
//LINQ
}
我一直遇到:
无法将类型'AnonymousType#1 []'隐式转换为'string []'
用下面的代码
var employees = from e in db.t_bisGames
group e by e.c_Initials into egrp
let max = egrp.Max(scor => scor.c_Score)
select new
{
Name = egrp.First(val => val.c_Score == max).c_Initials.ToArray(),
Score = egrp.First(val => val.c_Score == max).c_Score.ToString().ToArray()
};
问题是您的查询返回2个项目,而不仅仅是一个字符串。您的LINQ可能会执行相同的操作,并返回包含缩写和MaxScore
as属性的匿名类型。
您可以通过最后的第二个映射操作(Select
)将其转换为字符串数组:
public string[] GetMyGameList()
{
var query = db.BisGame.GroupBy(bg => bg.Initials)
.Take(10)
.Select(g => new { Initial = g.Key, MaxScore = g.Max(bg => bg.Score) });
// Convert to string[] somehow, ie:
return query.AsEnumerable().Select(i => string.Format("{0}:{1}", i.Initial, i.MaxScore)).ToArray();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句