How to search in 2D array by LINQ ?[version2]
You can do use the Enumerable.Range
method to generate a sequence of integers, and then use Linq to query over that.
Something like this would work:
string color = Enumerable
.Range(0, ClassNames.GetLength(0))
.Where(i => ClassNames[i, 0] == className)
.Select(i => ClassNames[i, 1])
.FirstOrDefault() ?? "Black";
Or in query syntax:
string color =
(from i in Enumerable.Range(0, ClassNames.GetLength(0))
where ClassNames[i, 0] == className
select ClassNames[i, 1])
.FirstOrDefault() ?? "Black";
Or perhaps convert the array to a Dictionary<string, string>
first:
Dictionary<string, string> ClassNamesDict = Enumerable
.Range(0, ClassNames.GetLength(0))
.ToDictionary(i => ClassNames[i, 0], i => ClassNames[i, 1]);
And then you can query it much more easily:
color = ClassNamesDict.ContainsKey(className)
? ClassNamesDict[className]
: "Black";
Generating the dictionary first and then querying it will be far more efficient if you have to do a lot of queries like this.
Here you are:
color = ClassNames.Cast<string>()
.Select((x, i) => new { x, i })
.GroupBy(x => x.i / 2, (k,x) => x.Select(y => y.x))
.Where(g => g.First() == className)
.Select(x => x.Last()).First();
But to be honest, I would never use LINQ to do that. It's less efficient, less readable and worse to maintain. You should consider using your existing for
loops or change your data structure, to be List<CustomClass>
or Dictionary<string, string>
instead of string[,]
.