How to create a new list from a list of the list, where the items are in the new list, are in an alternate order?

Suppose I have a list list. I want to create a new list from the list above so that the items are in the order of example below.

Inputs: -

List<List<int>> l = new List<List<int>>();

List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
List<int> b = new List<int>();
b.Add(11);
b.Add(12);
b.Add(13);
b.Add(14);
b.Add(15);
b.Add(16);
b.Add(17);
b.Add(18);

l.Add(a);
l.Add(b);

Output (list): -

1
11
2
12
3
13
4
14
15
16 

And the output list should contain not contain more than 10.

I am currently doing this with help foreach inside while, but I want to know how to do it using LINQ.

int loopCounter = 0,index=0;
List<int> o=new List<int>();
while(o.Count<10)
{
    foreach(List<int> x in l)
    {
        if(o.Count<10)
           o.Add(x[index]);
    }
    index++;
}

Thank.

+4
source share
3 answers

SelectMany Select, . . SelectMany . , Take :

var result = l.SelectMany((nested, index) => 
                  nested.Select((item, nestedIndex) => (index, nestedIndex, item)))
              .OrderBy(i => i.nestedIndex)
              .ThenBy(i => i.index)
              .Select(i => i.item)
              .Take(10);

:

var result = (from c in l.Select((nestedCollection, index) => (nestedCollection, index))
              from i in c.nestedCollection.Select((item, index) => (item, index))
              orderby i.index, c.index
              select i.item).Take(10);

# 6.0 :

var result = l.SelectMany((nested, index) => 
                  nested.Select((item, nestedIndex) => new {index, nestedIndex, item}))
              .OrderBy(i => i.nestedIndex)
              .ThenBy(i => i.index)
              .Select(i => i.item)
              .Take(10);

, Zip : zip join , join by - . , , .

- left join, ( ) . , OP full outer join - , , .

+3

, LINQ, , LINQ - , , , - . LINQ, /, , - / :

List<int> r = new List<int>(10);
for(int i = 0; i < 10; i++){
  if(i < a.Count)
    r.Add(a[i]);
  if(i < b.Count)
    r.Add(b[i]);
}

, a b 8 , for

, , LINQ,

LINQ ( , LINQ), , , - , , LINQ

+2

2 List<List<int>> - IEnumerable<int> yield, .ToList() , . Linq.Any .

, null. .

static IEnumerable<int> FlattenZip (List<List<int>> ienum, int maxLength = int.MaxValue)
{
  int done = 0;
  int index = 0;
  int yielded = 0;

  while (yielded <= maxLength && ienum.Any (list => index < list.Count))
    foreach (var l in ienum)
    {
      done++;

      if (index < l.Count)
      {
        // this list is big enough, we will take one out
        yielded++;
        yield return l[index];
      }

      if (yielded > maxLength)
        break; // we are done

      if (done % (ienum.Count) == 0)
        index += 1; // checked all lists, advancing index
    }
}

public static void Main ()
{
  // other testcases to consider: 
  //   in total too few elememts
  //   one list empty (but not null)
  //   too many lists (11 for 10 elements) 

  var l1 = new List<int> { 1, 2, 3, 4 };
  var l2 = new List<int> { 11, 12, 13, 14, 15, 16 };
  var l3 = new List<int> { 21, 22, 23, 24, 25, 26 };

  var l = new List<List<int>> { l1, l2, l3 };

  var zipped = FlattenZip (l, 10);

  Console.WriteLine (string.Join (", ", zipped));
  Console.ReadLine ();
}
0

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


All Articles