How to create new list from list of list where elements are in new list are in alternative order?

yajiv

Suppose I have list of list. I want to create new list from given list of list such that elements are in order of example given 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 output list must not contain more than 10 elements.

I am currently doing this using foreach inside while but I want to know how can I do this 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++;
}

Thanks.

Gilad Green

Use the SelectMany and Select overloads that receive the item's index. That will be used to apply the desired ordering. The use of the SelectMany is to flatten the nested collections level. Last, apply Take to retrieve only the desired number of items:

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);

Or in query syntax:

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);

If using a C# 6.0 and prior project an anonymous type instead:

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);

To explain why Zip alone is not enough: zip is equivalent to performing a join operation on the second collection to the first, where the attribute to join by is the index. Therefore Only items that exist in the first collection, if they have a match in the second, will appear in the result.

The next option is to think about left join which will return all items of the first collection with a match (if exists) in the second. In the case described OP is looking for the functionality of a full outer join - get all items of both collection and match when possible.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can i create a list (new) from an existing list where items of list(new) will be list in python

分類Dev

How to collect a new list from a list in python

分類Dev

Iterate over a list of list to create a new list of list

分類Dev

Relative order of elements in list

分類Dev

combine elements of a list together forming new lists

分類Dev

How to get items out of a list and create a new one in Ansible

分類Dev

How do I Create a new page by clicking on my list?

分類Dev

How to create a program white list for a new user account?

分類Dev

Create new list based on 'for' loop variable

分類Dev

Create new ProjectTemplate and not found into project template list

分類Dev

Reusing a list in a new function

分類Dev

Order a list of elements with another list of doubles

分類Dev

Create a list in a list from a file

分類Dev

Create vectors by extracting elements from list in R

分類Dev

merge common elements of a list of dictionary and store uncommon elements in a new key

分類Dev

Creating a Dictionary from a Key List and Item List Based on the Order of the Elements in the Item List

分類Dev

Python: Compare list of strings to list of tuples, and create a new list based on match or no match

分類Dev

Use list comprehension to return a new list

分類Dev

C# Nested List not adding a new list

分類Dev

Prolog how to make a new list of numbers

分類Dev

How to add new string element to a list of strings?

分類Dev

How to print nested list as new line in CSV?

分類Dev

How to bind new list via Html

分類Dev

Taking Elements From a List

分類Dev

How to add column to a dataframe from a list preserving the order of the list

分類Dev

Where are std::list elements allocated?

分類Dev

how to find elements from a list that are not present in another list in r

分類Dev

Making new list based of if internal nested elements are the same in python

分類Dev

Parsing elements from list of list of strings

Related 関連記事

  1. 1

    How can i create a list (new) from an existing list where items of list(new) will be list in python

  2. 2

    How to collect a new list from a list in python

  3. 3

    Iterate over a list of list to create a new list of list

  4. 4

    Relative order of elements in list

  5. 5

    combine elements of a list together forming new lists

  6. 6

    How to get items out of a list and create a new one in Ansible

  7. 7

    How do I Create a new page by clicking on my list?

  8. 8

    How to create a program white list for a new user account?

  9. 9

    Create new list based on 'for' loop variable

  10. 10

    Create new ProjectTemplate and not found into project template list

  11. 11

    Reusing a list in a new function

  12. 12

    Order a list of elements with another list of doubles

  13. 13

    Create a list in a list from a file

  14. 14

    Create vectors by extracting elements from list in R

  15. 15

    merge common elements of a list of dictionary and store uncommon elements in a new key

  16. 16

    Creating a Dictionary from a Key List and Item List Based on the Order of the Elements in the Item List

  17. 17

    Python: Compare list of strings to list of tuples, and create a new list based on match or no match

  18. 18

    Use list comprehension to return a new list

  19. 19

    C# Nested List not adding a new list

  20. 20

    Prolog how to make a new list of numbers

  21. 21

    How to add new string element to a list of strings?

  22. 22

    How to print nested list as new line in CSV?

  23. 23

    How to bind new list via Html

  24. 24

    Taking Elements From a List

  25. 25

    How to add column to a dataframe from a list preserving the order of the list

  26. 26

    Where are std::list elements allocated?

  27. 27

    how to find elements from a list that are not present in another list in r

  28. 28

    Making new list based of if internal nested elements are the same in python

  29. 29

    Parsing elements from list of list of strings

ホットタグ

アーカイブ