简体   繁体   中英

How to combine values of several lists into one in C#?

I'm trying to merge several values of diffrent lists into one line.

for example:

  • list A = [1,2,3,4,5,6,7,8,9]
  • list B = [A,B,C,D]
  • list C = [,?,,-]

then ill go with a loop through all lists and the output should be:

  • line = [1,A,!]
  • line = [2,B,?]
  • line = [3,C,-]
  • line = [4,D,NULL]
  • line = [5,NULL, NULL]
  • line = [6,NULL,NULL]...

The result will be added into one object

So far I tried to iterate through my lists with foreach loops but after I debugging it's clear that my approach cant work:

foreach (var item in list1){
       foreach (var item2 in list2){
             foreach (var item3 in list3){
                      string line = makeStringFrom(item, item2, item3);
             }
       }
} 

But I dont know how to make it work.

You can also use LINQ functions.

var listA = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var listB = new List<string> { "A", "B", "C", "D" };
var listC = new List<string> { "!", "?", "-" };

var result = Enumerable.Range(0, Math.Max(Math.Max(listA.Count, listB.Count), listC.Count))
    .Select(i => new
    {
        a = listA.ElementAtOrDefault(i),
        b = listB.ElementAtOrDefault(i),
        c = listC.ElementAtOrDefault(i)
    }).ToList();

foreach (var item in result)
{
    Console.WriteLine("{0} {1} {2}", item.a, item.b, item.c);
}

Result:

1 A !
2 B ?
3 C -
4 D
5
6
7
8
9

The general method would be:

  • Find the maximum length of all of the lists

  • Then create a loop to go from 0 to the max length-1

  • Check if each list contains that index of the item, and if so, retrieve the value, otherwise return null

  • Build your line from those values

You can use this:

var A = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
var B = new List<string>() { "A", "B", "C", "D" }; 
var C = new List<string>() { "!", "?", "-"};

var lists = new List<List<string>>() { A, B, C };

int count = 0;
foreach ( var list in lists )
  count = Math.Max(count, list.Count);

var result = new List<List<string>>();
for ( int index = 0; index < count; index++ )
{
  var item = new List<string>();
  result.Add(item);
  foreach ( var list in lists )
    item.Add(index < list.Count ? list[index] : null);
}

foreach ( var list in result )
{
  string str = "";
  foreach ( var item in list )
    str += ( item == null ? "(null)" : item ) + " ";
  str.TrimEnd(' ');
  Console.WriteLine(str);
}

We create a list of the lists so you can use that for any number of lists.

Next we take the max count of these lists.

Then we parse them as indicated by the algorithm:

  • We create a new list.
  • We add this list to the result that is a list of lists.
  • We add in this new list each of others lists items while taking null is no more items available.

You can use a StringBuilder if you plan to manage several and big lists to optimize memory strings concatenation.

Fiddle Snippet

Output

1 A !
2 B ?
3 C -
4 D (null)
5 (null) (null)
6 (null) (null)
7 (null) (null)
8 (null) (null)
9 (null) (null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM