简体   繁体   English

如何在 C# 中将多个列表的值合并为一个?

[英]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]列表 A = [1,2,3,4,5,6,7,8,9]
  • list B = [A,B,C,D]列表 B = [A,B,C,D]
  • list C = [,?,,-]列表 C = [,?,,-]

then ill go with a loop through all lists and the output should be:然后生病 go 循环遍历所有列表,并且 output 应该是:

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

The result will be added into one object结果将被添加到一个 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 循环遍历我的列表,但在调试之后很明显我的方法无法工作:

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.您还可以使用 LINQ 函数。

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然后创建一个循环到 go 从 0 到最大长度-1

  • Check if each list contains that index of the item, and if so, retrieve the value, otherwise return null检查每个列表是否包含该项目的索引,如果是,则检索该值,否则返回 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.我们在这个新列表中添加了其他每个列表项目,而 null 没有更多可用项目。

You can use a StringBuilder if you plan to manage several and big lists to optimize memory strings concatenation.如果您计划管理多个大型列表以优化 memory 字符串连接,则可以使用 StringBuilder。

Fiddle Snippet小提琴片段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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