Combine all the lines within the list <List <line> with LINQ

My question is almost the same as this one , but the list size is n. How to concatenate all rows inside (n dimension list) using LINQ? List<List<List...<string>>


NOTE. Interested in both cases: n is knownor unknown

+4
source share
2 answers

Since the related question is marked as C #, so I am adding this answer with C # code.

, SelectMany(), . .

List<List<List<string>>> nestedList = new List<List<List<string>>>();
var result = new string(nestedList.SelectMany(x => x).SelectMany(x => x).SelectMany(x => x).ToArray());

, , . , . , ;) , .

using Microsoft.CSharp.RuntimeBinder;

//...

private static string ConcatAll<T>(T nestedList) where T : IList
{
    dynamic templist = nestedList;
    try
    {
        while (true)
        {
            List<dynamic> inner = new List<dynamic>(templist).SelectMany<dynamic, dynamic>(x => x).ToList();
            templist = inner;
        }
    }
    catch (RuntimeBinderException)
    {
        List<object> l = templist;
        return l.Aggregate("", (a, b) => a + b);
    }
}

private static void Main(string[] args)
{
    List<List<List<string>>> nestedList = new List<List<List<string>>>
    {
        new List<List<string>> {new List<string> {"Hello "}, new List<string> {"World "}},
        new List<List<string>> {new List<string> {"Goodbye "}, new List<string> {"World ", "End "}}
    };

    Console.WriteLine(ConcatAll(nestedList));
}

:

Hello World Goodbye World End

Update:

. , , .

private static string ConcatAll<T>(T nestedList) where T : IList
{
    dynamic templist = nestedList;
    while (templist.Count > 0 && !(templist[0] is char?))
    {
        List<dynamic> inner = new List<dynamic>(templist).SelectMany<dynamic, dynamic>(x =>
        {
            var s = x as string;
            if (s != null)
            {
                return s.Cast<dynamic>();
            }
            return x;
        }).ToList();
        templist = inner;
    }
    return new string(((List<object>) templist).Cast<char>().ToArray());
}
+5

:

 static IEnumerable<string> Flatten(IEnumerable enumerable)
 {
        foreach (object el in enumerable)
        {
            if (enumerable is IEnumerable<string>)
            {
                yield return (string) el;
            }
            else
            {
                IEnumerable candidate = el as IEnumerable;
                if (candidate != null)
                {
                    foreach (string nested in Flatten(candidate))
                    {
                        yield return nested;
                    }
                }
            }
        }
 }

:

 List<List<List<string>>> nestedList = new List<List<List<string>>>
                                       {
                                          new List<List<string>> {new List<string> {"Hello "}, new List<string> {"World "}},
                                          new List<List<string>> {new List<string> {"Goodbye "}, new List<string> {"World ", "End "}}
                                      };

Console.WriteLine(String.Join(" ",Flatten(nestedList))); 

.

+3

Source: https://habr.com/ru/post/1616807/


All Articles