C#LINQ基于对象唯一性压缩2个列表列表

问题描述 投票:0回答:1

我正在尝试压缩对象列表的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# linq nested-lists
1个回答
0
投票

对于您的测试示例,我尝试了此代码,效果很好

List<List<int>> fullList = new List<List<int>>
{
    new List<int> { 1, 2, 3 },
    new List<int> { 2, 5, 6 },
    new List<int> { 7, 8, 9 }
};

List<List<int>> comboList = new List<List<int>>
{
    new List<int> { 2,5 },
    new List<int> { 8,9 }
};

foreach(var full in fullList)
foreach(var combo in comboList)
{
    if (!full.Any(f => combo.Any(c => c == f)))
    {
        full.AddRange(combo);
    }
}

希望对您有帮助


0
投票

var fullList = new List>()

        {

           new List<int>() {1,2,3},

            new List<int>() {2,5,6},

            new List<int>() {7,8,9}

        };



        var comboList = new List<List<int>>()

        {

            new List<int>() {2,5},

            new List<int>() {8,9},

        };





        fullList.ForEach(i =>

        {

            comboList.ForEach(j =>

            {

                if (i.All(x => !j.Contains(x)))

                {

                    i.AddRange(j);

                    return;

                };

            });

        });

0
投票

好吧,听起来您可能想要something喜欢:

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;
    }                                           
}
© www.soinside.com 2019 - 2024. All rights reserved.