Rambo Vincent

Rambo Vincent

  • NA
  • 14
  • 1.9k

Linq access to nested list of objects via index

Nov 4 2015 5:51 PM
I have a List of LinePieces which each contain a list of GamePieces. I'm attempting to, based on the direction of the move made by user, x or y, get the line(s) that are in that directions axis of the piece moved.

For example, say I moved piece A to piece B then I'll need to check the List of Lists for all pieces in the nested list that have a transform.position.y == 1, if I moved B to E then I'll need to check the List of Lists for all pieces in the nested list that have a transform.position.x == 2. Ill need to return to a List<LinePieces>

Y Y Y
1 2 3
X 1 A D G
X 2 B E H
X 3 C F I

My current code is this but it doesn't seem to be working, giving me exception (ArgumentOutOfRangeException) and it's not running the code after it when it gives this exception. I guess I'm trying to use i as an index for the inner list, but i is actual the index for the outer list....

bool IsYMove = xDirIndex1 == xDirIndex2 ? false : true;

List<LinePieces> lineDataSet = new List<LinePieces> ();
lineDataSet = IsYMove == false
? _board.Lines.Where((x, i) => x.Line[i].transform.position.x == xDirIndex1).ToList()
: _board.Lines.Where((y, i) => y.Line[i].transform.position.y == yDirIndex1).ToList();
// In the above LINQ query I believe I'm trying to use the i index from the outer List in accessing the Line[i] item in the inner list, which may be causing my issue but I don't know how to make an index for the inner list

// If IsYMove == false (Column switch), check the entire line where .x == xDirIndex1
// ...

foreach (LinePieces line in lineDataSet)
{
............
}


_board.Lines is a List<LinePiece> and LinePieces.Line (x.Line and y.Line in the linq query) is a List<GamePiece>.

I need help getting this code right or an alternative method to perform this action.