选择在LINQ多维阵列 [英] Selecting a multi-dimensional array in LINQ

查看:127
本文介绍了选择在LINQ多维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我需要一个DataTable转换为一个二维数组的任务。这是很容易,只需遍历的行和列(见下面的例子)来完成。

I have a task where I need to translate a DataTable to a two-dimensional array. That's easy enough to do by just looping over the rows and columns (see example below).

private static string[,] ToArray(DataTable table)
{
    var array = new string[table.Rows.Count,table.Columns.Count];

    for (int i = 0; i < table.Rows.Count; ++i)
        for (int j = 0; j < table.Columns.Count; ++j)
            array[i, j] = table.Rows[i][j].ToString();

    return array;
}



我真正想要做的是使用LINQ SELECT语句生成二维数组。不幸的是,它看起来像没有在LINQ没有办法选择一个多维数组。是的,我知道,我可以使用LINQ选择一个交错数组,但是这不是我想要的。

What I'd really like to do is use a select statement in LINQ to generate that 2D array. Unfortunately it looks like there is no way in LINQ to select a multidimensional array. Yes, I'm aware that I can use LINQ to select a jagged array, but that's not what I want.

是我的假设是正确的,或者是有办法使用LINQ选择一个多维数组?

Is my assumption correct, or is there a way to use LINQ to select a multi-dimensional array?

推荐答案

我不认为这是可能的。我的理由是,选择以及大多数其他LINQ功能要求,他们对工作的藏品至少实现的IEnumerable< T> 对于一些T:

I don't think it is possible. My reasoning is that Select and most other LINQ functions require that the collections they work on implement at least IEnumerable<T> for some T:

public static IEnumerable<TResult> Select<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, TResult> selector
)

一个矩形阵列不执行的IEnumerable< T> 对于任意t,因此不能返回值一个选择功能。

A rectangular array doesn't implement IEnumerable<T> for any T so it can't be the return value of a Select function.

这篇关于选择在LINQ多维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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