【发布时间】:2013-09-11 12:01:40
【问题描述】:
我有一个像这样的 2D 数组:
string[,] ClassNames =
{
{"A","Red"},
{"B","Blue"},
{"C","Pink"},
{"D","Green"},
{"X","Black"},
};
我通过 for 语句在 1nd 列中搜索 ClassName 并在 2nd 中返回 ColorName strong> 像这样的列:
string className = "A";
string color = "Black";
for (int i = 0; i <= ClassNames.GetUpperBound(0); i++)
{
if (ClassNames[i, 0] == className)
{
color = ClassNames[i, 1];
Response.Write(color);
break;
}
}
我想使用 LINQ 而不是 for 语句来获取 className 的 color。 如何将上面的 for 语句转换为 LINQ。
【问题讨论】:
-
您的二维数组看起来应该是字典?
-
二维数组实际上是
c的东西,在c#中我们应该使用其他集合结构/类。
标签: c# asp.net arrays linq multidimensional-array