【问题标题】:How to add add items near to next list如何在下一个列表附近添加添加项目
【发布时间】:2020-08-29 13:41:01
【问题描述】:

我正在尝试制作一个类似单词表生成器的程序。 我想在第一个列表中的每个项目旁边添加第二个列表中的项目。 `

List<string> list1 = new List<string>() {"tomato", "ball", "icecream", "blue"};

List<string> list2 = new List<string>() { "123", "yellow", "green" };

`

//Values ​​to be added to Listing 3: tomato123, tomatoyellow, tomatogreen, ball123, ballyellow, ballgreen bla bla bla

【问题讨论】:

标签: c# linq


【解决方案1】:

为了解决您的问题,我们将遍历其中一个列表,并且对于其中的每个项目,我们将使用另一个列表中的单词创建所有可能的组合。使用 LINQ,它看起来像这样:

var list3 = list1.Select(w1 => list2.Select(w2 => w1 + w2)).ToList();

问题是现在list3List&lt;IEnumerable&lt;string&gt;&gt; 类型,因为我们有一个list1 中每个单词的组合列表。为了展平结果,我们只需将Select 投影更改为SelectMany 展平投影:

var list3 = list1.SelectMany(w1 => list2.Select(w2 => w1 + w2)).ToList();

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
  • @MarkRotteveel 感谢您的反馈,我更新了答案以包含更多信息。
【解决方案2】:

根据您的要求,它可能会有用。请看一下。

 static void AddLists()
    {
        List<string> list1 = new List<string>() { "tomato", "ball", "icecream", "blue" };
        List<string> list2 = new List<string>() { "123", "yellow", "green" };

        var resultList = from l1 in list1
                         from l2 in list2
                         select string.Concat(l1, l2);           
    }

【讨论】:

    【解决方案3】:

    我的建议是创建一个扩展方法,而不是一个难以理解的 LINQ 语句:读者会立即看到它的作用,它更容易测试和更改。

    extension methods demystified

    public static IEnumerable<string> ConcatCombinations(
        this.IEnumerable<string> sequenceA,
        IEnumerable<string> sequenceB)
    {
        // TODO: invent a proper name
    
        foreach (string textA in sequenceA)
            foreach (string textB in sequenceB)
                yield return textA + textB;
    }
    

    此代码比使用 LINQ 方法的任何解决方案都简单得多。任何人都会立即看到它的作用。

    用法:

    List<string> list1 = ...
    string[] array1 = ...
    
    List<string> concatenated = list1.ConcatCombinations(array1).ToList();
    

    如果你想做一个更通用的方法,考虑这个:

    public static IEnumerable<TResult> MakeCombinations<TA, TB, TResult>(
        this IEnumerable<TA> itemsA,
        IEnumerable<TB> itemsB,
        Func<TA, TB, TResult> resultSelector)
    {
        foreach (TA itemA in itemsA)
            foreach (TB itemB in itemsB)
            {
                TResult result = resultSelector(itemA, itemB);
                yield return result;
            }
    }
    

    用法:

    List<string> list1 = ...
    List<string> list2 = ...
    
    List<string> concatenated = list1.ConcatCombinations(list2,
    
       // parameter ResultSelector: concat every a and b:
       (a, b) => a+b)
       .ToList();
    

    或者只是改变你的 ConcatCombinations:

    public static IEnumerable<string> ConcatCombinations(
        this.IEnumerable<string> sequenceA,
        IEnumerable<string> sequenceB)
    {
        return sequenceA.MakeCombinations(sequenceB,
            (a, b) => a + b);
    }
    

    另一个完全不同的例子,它向您展示了代码的可重用性:

    var maleBallroomDancers = ...
    var femaleBallroomDancers = ...
    
    var danceCombinations = maleBallroomDancers.MakeCombinations(femaleBallroomDancers,
    
        (male, female) => new
        {
            Male = male,
            Female = female,
        })
        .ToList();
    

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      相关资源
      最近更新 更多