【发布时间】:2011-09-22 02:38:56
【问题描述】:
我的大脑无法正常工作,我正在尝试抓取该网格上的前三行。我正在制作一个简单的跳棋游戏,只是为了学习一些新东西。我的代码正在抓取前三列来初始化红色棋子的位置。我想要前三行。
这就是我的代码现在正在做的事情:
这是我的(简化的)代码。 Square 是我的一类,它只保存一些小物品来跟踪碎片。
private Square[][] m_board = new Square[8][];
for (int i = 0; i < m_board.Length; i++)
m_board[i] = new Square[8];
//find which pieces should hold red pieces, the problem line
IEnumerable<Square> seqRedSquares =
m_board.Take(3).SelectMany(x => x).Where(x => x != null);
//second attempt with the same result
//IEnumerable<Square> seqRedSquares =
m_board[0].Union(m_board[1]).Union(m_board[2]).Where(x => x != null);
//display the pieces, all works fine
foreach (Square redSquare in seqRedSquares)
{
Piece piece = new Piece(redSquare.Location, Piece.Color.Red);
m_listPieces.Add(piece);
redSquare.Update(piece);
}
【问题讨论】:
-
请不要在标题前加上“C# LINQ”。这已经在标签中了。
-
@John Saunders,无论哪种方式,我都没有强烈的意见,但是如果您浏览问题标题列表,是否更容易在心理上进行排序?
-
Stack Overflow 上的人们习惯于使用标签来过滤问题列表,所以不,它没有。它只会使问题标题变得丑陋且难以阅读。自己阅读:“C# LINQ 获取锯齿状数组的前三个元素”与“获取锯齿状数组的前三个元素”。哪一个有意义并告诉您有关问题的一些信息,哪一个只是前面有东西?
-
@JohnSaunders,啊,我明白了,你说得很好。我以后会写干净的标题。
标签: c# winforms linq jagged-arrays