Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有效跳过二维字符串数组中的空行_C#_Linq_Multidimensional Array - Fatal编程技术网

C# 有效跳过二维字符串数组中的空行

C# 有效跳过二维字符串数组中的空行,c#,linq,multidimensional-array,C#,Linq,Multidimensional Array,考虑像这样的二维阵列: 使用以下代码将跳过空行: public static string[,] SkipBlankRows(this string[,] array2D) { var columns = array2D.GetLength(1); var rows = array2D.GetLength(0); var temp = new List<string[]>(); for (var r = 0; r < rows; r++)

考虑像这样的二维阵列:

使用以下代码将跳过空行:

public static string[,] SkipBlankRows(this string[,] array2D)
{
    var columns = array2D.GetLength(1);
    var rows = array2D.GetLength(0);
    var temp = new List<string[]>();

    for (var r = 0; r < rows; r++)
    {
        var row = new string[columns];
        for (var c = 0; c < columns; c++)
        {
            row[c] = array2D[r, c];
        }
        if (row.All(itm => string.IsNullOrWhiteSpace(itm)))
            continue;
        temp.Add(row);
    }

    string[,] result = new string[temp.Count(), columns];
    rows = temp.Count();

    for (int r = 0; r < rows; r++)
    {
        var row = temp[r];
        for (var c = 0; c < row.Length; c++)
        {
            result[r,c]=row[c];
        }
    }
    return result;
}
结果:

结果应该是<代码> 2D数组的字符串< /代码>,其中空白行将不存在。


<> >代码看起来很笨拙,有可能做得更好吗?例如,涉及LINQ?

< P>这取决于你希望你的输出看起来像什么,你只想跳过空白字符串并有一个值列表吗?还是希望数据仍在多维数组中

如果您对这个问题的回答是“第二个”,那么您的代码就可以了

如果您只需要多维数组中所有值的列表,可以编写如下内容:

public static IEnumerable<string> SkipBlankRows(this string[,] array2D)
{
    return (from string s in array2D where !string.IsNullOrWhiteSpace(s) select s);
}
公共静态IEnumerable SkipBlankRows(此字符串[,]array2D)
{
返回(从array2D中的字符串s,其中!string.IsNullOrWhiteSpace选择s);
}
这将从数组返回值的平面结构


希望这有帮助

您可以使用
LINQ
字符串[,]
转换为
IEnumerable
,删除空行,然后将
IEnumerable
放回
字符串[,]
。我不知道使用
LINQ
IEnumerable
投影到
字符串[,]
,所以我只使用嵌套的
foreach
循环

public static string[,] SkipBlankRows(this string[,] array2D)
{
    int columnCount = array2D.GetLength(1);

    var withoutEmptyLines = array2D
        .Cast<string>()  // Flatten the 2D array to an IEnumerable<string>
        .Select((str, idx) => new { str, idx }) // Select the string with its index
        .GroupBy(obj => obj.idx / columnCount) // Group the items into groups of "columnCount" items
        .Select(grp => grp.Select(obj => obj.str)) // Select the groups into an IEnumerable<IEnumerable<string>>
        .Where(strs => !strs.All(str => string.IsNullOrWhiteSpace(str))); // Filter empty rows;

    // Put the IEnumerable<IEnumerable<string>> into a string[,].
    var result = new string[withoutEmptyLines.Count(), columnCount];
    int rowIdx = 0;
    foreach (var row in withoutEmptyLines)
    {
        int colIdx = 0;
        foreach (var col in row)
        {
            result[rowIdx, colIdx++] = col;
        }
        rowIdx++;
    }

    return result;
}
公共静态字符串[,]SkipBlankRows(此字符串[,]array2D)
{
int columnCount=array2D.GetLength(1);
var WithoutEmptyline=array2D
.Cast()//将2D数组展平为IEnumerable
.Select((str,idx)=>new{str,idx})//选择带索引的字符串
.GroupBy(obj=>obj.idx/columnCount)//将项目分组为“columnCount”项目组
.Select(grp=>grp.Select(obj=>obj.str))//将组选择到IEnumerable中
.Where(strs=>!strs.All(str=>string.IsNullOrWhiteSpace(str));//筛选空行;
//将IEnumerable放入字符串[,]。
var result=新字符串[withoutEmptyLines.Count(),columnCount];
int rowIdx=0;
foreach(WithoutEmptyline中的变量行)
{
int colIdx=0;
foreach(行中的变量列)
{
结果[rowIdx,colIdx++]=col;
}
rowIdx++;
}
返回结果;
}

LINQ旨在处理和生成集合,而不是多维数组。您可以将第一个
for
循环替换为更具表现力的LINQ,但您无法真正摆脱使用
for
循环来重新填充新数组的问题:

public static string[,] SkipBlankRows(this string[,] array2D)
{
    var columns = array2D.GetLength(1);
    var rows = array2D.GetLength(0);
    var temp = Enumerable.Range(0, rows)
        .Select(i => Enumerable.Range(0, columns).Select(j => array2D[i, j]).ToList())
        .Where(row => !row.All(string.IsNullOrEmpty))
        .ToList();

    string[,] result = new string[temp.Count, columns];
    rows = temp.Count;

    for (int r = 0; r < rows; r++)
    {
        var row = temp[r];
        for (var c = 0; c < row.Count; c++)
        {
            result[r, c] = row[c];
        }
    }
    return result;
}
公共静态字符串[,]SkipBlankRows(此字符串[,]array2D)
{
var columns=array2D.GetLength(1);
var rows=array2D.GetLength(0);
var temp=可枚举范围(0,行)
.Select(i=>Enumerable.Range(0,列)。Select(j=>array2D[i,j]).ToList()
.Where(row=>!row.All(string.IsNullOrEmpty))
.ToList();
字符串[,]结果=新字符串[临时计数,列];
行=临时计数;
对于(int r=0;r
当然,如果您愿意引入几个helper方法来抽象行与行之间的转换,那么最终可以得到一个高效且易于阅读的代码


也许有必要解释一下您希望输出看起来像什么样的答案!我只想删除(跳过)空行并返回字符串的
2D数组
。感谢您的回答,有趣的linq用法!我可能会继续使用我的原始代码以保持可读性,但很高兴看到linq是如何强大。谢谢你的回答,有趣的linq用法!我可能会继续使用我的原始代码以保持可读性,但很高兴看到linq是多么强大。
public static string[,] SkipBlankRows(this string[,] array2D)
{
    var columns = array2D.GetLength(1);
    var rows = array2D.GetLength(0);
    var temp = Enumerable.Range(0, rows)
        .Select(i => Enumerable.Range(0, columns).Select(j => array2D[i, j]).ToList())
        .Where(row => !row.All(string.IsNullOrEmpty))
        .ToList();

    string[,] result = new string[temp.Count, columns];
    rows = temp.Count;

    for (int r = 0; r < rows; r++)
    {
        var row = temp[r];
        for (var c = 0; c < row.Count; c++)
        {
            result[r, c] = row[c];
        }
    }
    return result;
}