使用LINQ从String数组中删除"NULL行"

cll*_*pse 2 c# linq arrays

如何使用LINQ从String数组中删除"NULL行"?

采取这种结构(String[,]):

"Hello", "World", "Foo", "Bar"
null,    null,    null,  null
null,    null,    null,  null
"Hello", "World", "Foo", "Bar"
"Hello", "World", "Foo", "Bar"
null,    null,    "Foo", "Bar"
Run Code Online (Sandbox Code Playgroud)

应删除第一行之后的两行.结构中的最后一行不应该.

bru*_*nde 5

例如,如果列表中有数组,则可以执行以下操作:

        IList<string[]> l = new List<string[]>
        {
            new []{ "Hello", "World", "Foo", "Bar" },
            new string[]{ null,    null,    null,  null },
            new string[] { null,    null,    null,  null },
            new [] { "Hello", "World", "Foo", "Bar" },
            new [] {null, null, "Foo", "Bar" }
        };
        var newList = l.Where(a => a.Any(e => e != null));
Run Code Online (Sandbox Code Playgroud)

(更新)

我不认为Linq会为你提供多维数组的帮助.这是一个使用普通旧for循环的解决方案......

        string[,] arr = new string[,] {
            { "Hello", "World", "Foo", "Bar" },
            { null,    null,    null,  null },
            { null,    null,    null,  null },
            { "Hello", "World", "Foo", "Bar" },
            {null, null, "Foo", "Bar" }
        };

        IList<string[]> l = new List<string[]>();

        for (int i = 0; i < arr.GetLength(0); i++)
        {
            string[] aux = new string[arr.GetLength(1)];
            bool isNull = true;
            for (int j = 0; j < arr.GetLength(1); j++)
            {
                aux[j] = arr[i, j];
                isNull &= (aux[j] == null);
            }
            if (!isNull)
                l.Add(aux);
        }
Run Code Online (Sandbox Code Playgroud)

这导致了List<string[]>.