crm*_*ham 0 c# linq ienumerable xna list
我有一个string[]包含数组的列表.
数组包含两个元素,[0] =得分,[1] =难度(1或2).
我使用以下LINQ语句按难度为2的降序按分数重新排序列表.
scoresDesHard = list.OrderByDescending(ld => lineData[0]).Where(ld => lineData[1] == "2");
Run Code Online (Sandbox Code Playgroud)
我目前正在将新排序的列表绘制到屏幕中,XNA如下所示:
// draw highscores to the screen
public void Draw(SpriteBatch spriteBatch)
{
string mainString = "";
// build the hard list string
foreach (var li in scoresDesHard)
{
mainString += li[0] + " " + li[1] + "\r\n";
}
spriteBatch.DrawString(scoreFont, ""+ mainString, hardScoresPos, Color.White);
}
Run Code Online (Sandbox Code Playgroud)
它根本没有订购列表,并显示两个困难的分数:
000001 1
000001 2
122122 1
125555 1
22 1
23131 2
Run Code Online (Sandbox Code Playgroud)
它应该输出:
23131 2
00001 2
Run Code Online (Sandbox Code Playgroud)
为什么没有订购清单?
我认为问题是你将ld传递给Lambda,但检查lineData[1] == 2我认为LINQ语句应该是这样的
scoresDesHard = list.Where(x => x[1] == "2").OrderByDescending(y => y[0]);
list.Where(foo => bar.Value1 == 123) //Always true or always false
list.Where(foo => foo.Value1 == 123) //checks each item in the list
Run Code Online (Sandbox Code Playgroud)