I'd rather would go for something like this:
public class CellInfo
{
public int Row { get; set; }
public int Column { get; set; }
public string Value { get; set; }
}
Example:
List<CellInfo> _cells = new List<CellInfo>();
public void Example()
{
_cells.Add(new CellInfo { Column = 6, Row = 3, Value = "Rak" });
_cells.Add(new CellInfo { Column = 3, Row = 8, Value = "Rak" });
_cells.Add(new CellInfo { Column = 2, Row = 4, Value = "Rak" });
_cells.Add(new CellInfo { Column = 5, Row = 7, Value = "Sac" });
_cells.Add(new CellInfo { Column = 1, Row = 3, Value = "Sac" });
int[] rowRakAtColumn0 = _cells.Where(cell => cell.Column == 0
&& cell.Value.Contains("Rak"))
.OrderBy(cell => cell.Row)
.Select(cell => cell.Row)
.ToArray();
CellInfo[] row0Values = _cells.Where(cell => cell.Row == 0)
.OrderBy(cell => cell.Column)
.ToArray();
}
Converting it to a multi array (if needed)
public string[,] ToMultiArray(List<CellOffset> cells)
{
string[,] multiDimentional = new string[
cells.Max(cell => cell.Column), cells.Max(cell => cell.Row)];
cells.ForEach(cell =>
multiDimentional[cell.Column, cell.Row] = cell.Value);
return multiDimentional;
}
string[,] values = ToMultiArray(_cells);
Answer to lazzy_ms:
The linq .Select()
method is also able to pass the index as second parameter for your lambda function. You only need to capture it in an anonymous class.
var result = _cells
// here and capture it in an object
// |
.Select((Item, Index) => new { Item, Index })
.FirstOrDefault(itemWithIndex => itemWithIndex.Item.Column == 5);
Console.WriteLine($"The index of column 5 is {result?.Index}");
Make sure you select the index before any filtering takes place. else it wont match