You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何结合exclude规则合并分组结果生成最终宠物列表?

问题:合并分组结果与排除规则的最终实现

输入数据

我们有一组包含Pets数组和exclude字段的对象列表:

{    
    "Pets": [
        {
            "example": "cat",
            "category": "group1"  
        },
        {
            "example": "dog",
            "category": "group1"
        },
        {
            "example": "cow",
            "category": "group1"
        }
    ],
    "exclude": false
}
{    
    "Pets": [
        {
            "example": "crow",
            "category": "group2"            
        },
        {            
            "example": "cat",
            "category": "group2"
        }
    ],
    "exclude": true
}
{    
    "Pets": [
        {              
            "example": "cow",
            "category": "group3" 
        }
    ],
    "exclude": false
}

现有实现与问题

1. 按Example分组合并Category(OUTPUT1)

通过以下C#代码,可按example字段分组,将同一example对应的category合并为列表,得到OUTPUT1:

public class AnimalsGrouped
{
    public string Example { get; set; }
    public List<string> Categories { get; set; }
}

var pets = input.SelectMany(x => x.Pets).ToList(); // input为上述3个对象的列表
var listGrouped = pets.GroupBy(x => x.Example)
   .Select(x => new AnimalsGrouped() 
    { 
        Example = x.Key, 
        Categories = x.Select(y => y.Category).Distinct().ToList() 
    })
    .ToList();

对应的OUTPUT1:

[
    {
        "example": "cat",
        "categories": ["group1", "group2"]    
    },
    {
        "example": "dog",
        "categories": ["group1"] 
    },
    {
        "example": "cow",
        "categories": ["group1", "group3"]    
    },
    {
        "example": "crow",
        "categories": ["group2"] 
    }
]

2. 应用Exclude规则的现有逻辑(OUTPUT2)

根据规则:exclude为true时移除对应Pets,为false时保留对应Pets,现有代码如下:

var toInclude = input.Where(g => !g.exclude).SelectMany(x => x.Pets).ToList();
var toExclude = input.Where(g => g.exclude).SelectMany(x => x.Pets).ToList();
var diff = toInclude.Except(toExclude).ToList();

但这样得到的OUTPUT2存在分类信息缺失问题:

[
    {
        "example": "dog",
        "categories": ["group1"] 
    },
    {
        "example": "cow",
        "categories": ["group1"]    // 丢失了group3分类
    }
]

我们需要结合OUTPUT1和OUTPUT2的逻辑,生成包含完整分类信息的最终结果:

[
    {
        "example": "dog",
        "categories": ["group1"] 
    },
    {
        "example": "cow",
        "categories": ["group1", "group3"]    
    }
]

解决方案

核心思路是:先筛选出符合exclude规则的example列表,再从OUTPUT1的分组结果中提取这些example对应的完整分类信息。

实现代码如下:

// 第一步:获取所有符合exclude规则的有效example
var validExamples = input.Where(g => !g.exclude)
                        .SelectMany(x => x.Pets)
                        .Select(p => p.example)
                        .Except(input.Where(g => g.exclude)
                                    .SelectMany(x => x.Pets)
                                    .Select(p => p.example))
                        .ToHashSet();

// 第二步:从分组结果中筛选有效example,保留完整分类
var finalResult = listGrouped.Where(ag => validExamples.Contains(ag.Example)).ToList();

这样就能得到既遵守exclude规则、又保留每个example所有分类信息的最终结果。

内容的提问来源于stack exchange,提问作者user989988

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 18:45:42