我有这个二维 bool 矩阵 A
public bool[10][10] A;
是否有一个 Linq 可以执行以下方法的操作? (返回所有索引 i 以便对于给定的 n,A[i][n] 为真)
public List<int> getIndex (int n)
{
List<int> resp = new List<int>();
for (int i = 0; i < 10; i++)
if (A[i][n])
{
resp.Add(i);
}
return resp;
}
请您参考如下方法:
return Enumerable.Range(0, A.Length).Where(x => A[x][n]).ToList();
应该做的。否则你可以通过返回 IEnumerable<int> 来让整个事情变得懒惰。
public IEnumerable<int> getIndex (int n)
{
return Enumerable.Range(0, A.Length).Where(x => A[x][n]);
}
