如何在多维数组上使用 LINQ 来“展开"数组? [英] How to use LINQ on a multidimensional array to 'unwind' the array?

查看:15
本文介绍了如何在多维数组上使用 LINQ 来“展开"数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下数组:

int[,] numbers = new int[3, 2] { { 2, 1 }, { 3, 4 }, { 6, 5 } };

我想用 LINQ 构造一个带有数字 2、1、3、4、6、5 的 IEnumerable.

I would like to use LINQ to construct an IEnumerable with numbers 2, 1, 3, 4, 6, 5.

最好的方法是什么?

推荐答案

怎么样:

Enumerable
    .Range(0,numbers.GetUpperBound(0)+1)
    .SelectMany(x => Enumerable.Range(0,numbers.GetUpperBound(1)+1)
    .Select (y =>numbers[x,y] ));

或整理.

var xLimit=Enumerable.Range(0,numbers.GetUpperBound(0)+1);
var yLimit=Enumerable.Range(0,numbers.GetUpperBound(1)+1);
var result = xLimit.SelectMany(x=> yLimit.Select(y => numbers[x,y]));

编辑修改后的问题....

EDIT Revised Question....

var result = array.SelectMany(x => x.C);

这篇关于如何在多维数组上使用 LINQ 来“展开"数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆