首页 > 解决方案 > C# LINQ Zipping 2 基于对象唯一性的列表列表

问题描述

我正在尝试压缩 2 个对象列表列表 - 仅在生成的压缩列表包含不同对象列表的情况下(与列表 1 相比,列表 2 的列表有时具有重复的对象)。结果列表中的对象数量必须保持不变,这意味着我们不能有不同大小的列表。列表 1 中的列表数量大于列表 2 中的数量。这意味着在压缩时重复通过列表 2。

上面使用下面的循环表示。这可能只使用linq吗?

//this list of list contains the larger number of elements to which the 2nd list of list needs to be joined
List<List<object>> FullList = new List<List<object>>(); //is not empty - instantiated with some list

//2nd list of list - contains duplicates of the 1st list in terms of the objects in the underlying list
List<List<object>> comboBaseList = new List<List<object>>(); //is not empty - instantiated with some list
List<List<object>> comboList = comboBaseList;

//need to zip lists where there are no duplicate objects in the 2nd list's list, the count of the 1st list of list remains the same as FullList
List<List<object>> finalList = new List<List<object>>(); //empty - meant to hold final combined list

int i = 0;

foreach iList in FullList
{
    if (i < comboList.count)
    {
        while (iList.Select(x => x.Id).Contains(comboList[i].Select(y => y.Id)))
        {
            i += 1;
        }
        finalList.Add(iList.Zip(comboList[i], (x, y) => x.Union(y))); //**CAN WE BYPASS THE LOOPS AND JUST USE LINQ?
        comboList.RemoveAt(i);
        i = 0;
    }
    else
    {
        comboList = comboBaseList;
        i = 0;
    }
}

为了简化数据,我将使用整数列表列表——在我的例子中,这可能是对象的 ID

FullList = 
(
{1,2,3},
{2,5,6},
{7,8,9}
)

comboList = 
(
{2,5},
{8,9}
)

我想压缩上面的列表以产生如下结果 - 请注意,根据 FullList 有 3 个结果,并且不符合条件的列表具有不同的整数

finalList =
{
 {1,2,3,8,9},
 {2,5,6,8,9},
 {7,8,9,2,5}
}

标签: c#linqnested-lists

解决方案


好的,听起来你可能想要这样的东西

var result = fullList
    .Select(original =>
       // We want the original sublist...
       original
           // concatenated with...
           .Concat(
           // ... any comboList element
           comboList
               // which is completely distinct from "original"
               .Where(cl => !original.Intersect(cl).Any())
               // ... flattening all such comboLists
               .SelectMany(cl => cl))
           .ToList())
    .ToList();

这是一个完整的例子:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var fullList = new[]
        {
            new[] { 1, 2, 3 },
            new[] { 2, 5, 6 },
            new[] { 7, 8, 9 }
        };

        var comboList = new[]
        {
            new[] { 2, 5 },
            new[] { 8, 9 }
        };

        var result = MergeCombinations(fullList, comboList);
        foreach (var item in result)
        {
            Console.WriteLine(string.Join(", ", item));
        }
    }

    static List<List<T>> MergeCombinations<T>(
        IEnumerable<IEnumerable<T>> fullList,
        IEnumerable<IEnumerable<T>> comboList)
    {
        var result = fullList
            .Select(original =>
                // We want the original sublist...
                original
                    // concatenated with...
                    .Concat(
                    // ... any comboList element
                       comboList
                           // which is completely distinct from "original"
                           .Where(cl => !original.Intersect(cl).Any())
                           // ... flattening all such comboLists
                          .SelectMany(cl => cl))
                   .ToList())
             .ToList();
        return result;
    }                                           
}

推荐阅读