Using an Array
of arrays or a List
of arrays is much easier, but a 2D array can be done.
For a List
of arrays:
var listInput = File.RealAllLines("1.csv").Select(line => line.Split(';')).ToList();
To find any rows containing a string:
var find = "5549871";
var ContainingRows = listInput.Where(r => r.Any(s => s.Contains(find))).ToList();
To find a row containing an exact match:
var EqualRows = listInput.Where(r => r.Any(s => s == find)).ToList();
If you know there is just one match, you can replace ToList()
with First()
.
If you know more about your search, you could create an index (Dictionary
) instead to speed up retrieval.
Unfortunately, there aren't any corresponding 2D array creation features, you must know the size to create it.
var array2d = new string[listInput.Length, 3];
for (int row = 0; row < listInput.Length; ++row) {
for (int col = 0; col < 3; ++col)
array2d[row, col] = listInput[row][col];
}
Searching it isn't going to be fast unless you create some type of index, but also easy.
var findrow = -1;
for (int row = 0; row < array2d.GetUpperBound(0); ++row) {
for (int col = 0; col < array2d.GetUpperBound(1); ++col) {
if (array2d[row,col].Contains(find)) {
findrow = row;
break;
}
}
}