在n嵌套列表中递归清理重复项 [英] Recursive cleanup of duplicate items in n-nested list

查看:61
本文介绍了在n嵌套列表中递归清理重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个特定的场景,我正试图解决。我有一个嵌套的数据列表,每个数据都填充了一个服务列表(由代码标识)。


基本上我想要实现的是以下内容,我想创建一个递归遍历每个嵌套级别的方法,在该级别获取代码,然后检查子列表是否具有相同的代码,无论哪个匹配,然后从父级中删除它们。


请参阅代码示例数据设置:

公共类服务
{
public int Id {get;组; };
公共字符串代码{get;组; };
public List< Service>服务{get;组; } = new List< Service>();
}



int counter = 0;
var services = new List< Service>
{
新服务
{
Id = ++ counter,
Code =" SG1",
Services = new List< Service>
{
new Service {Id = ++ counter,Code =" S1"},
new Service {Id = ++ counter,Code =" S2"},
新服务{Id = ++ counter,Code =" S3"},
new Service {Id = ++ counter,Code =" S4"},
new Service {Id = ++ counter,Code =" S5"},
new service
{
Id = ++ counter,
Code =" SG2",
Services = new List< ;服务>
{

新服务{Id = ++ counter,Code =" S3"},
new Service {Id = ++ counter,Code =" S4"} ,
新服务{Id = ++ counter,Code =" S5"},
new service
{
Id = ++ counter,
Code =" ; SG3",
服务=新列表<服务>
{
new Service {Id = ++ counter,Code =" S3"},
new Service {Id = ++ counter,Code =" S4"},
}
}
}
}
}

}
};

我希望看到的输出是同一棵树,但父项删除了它们存在于子项中的位置,所以像这样:



SG1


---- S1


---- S2


---- SG2


-------- S5


- ------ SG3


------------ S3


-------- ---- S4

解决方案

考虑两个新功能:

公共类服务
{
public int Id {get;组; }
公共字符串代码{get;组; }
public List< Service>服务{get;组; } = new List< Service>();

public bool包含(字符串代码)
{
返回Services.Any(s => s.Code == code || s.Contains(code));
}

public void RemoveUnneeded()
{
Services.ForEach(s => s.RemoveUnneeded());
var to_remove = Services.Where(s => Services.Any(z => z!= s&& z.Contains(s.Code)));
to_remove.ToList()。ForEach(s => Services.Remove(s));
}
}





用法:

 var services = new List< Service> 
。 。 。
services.ForEach(s => s.RemoveUnneeded());





Hi,

I've got a specific scenario i'm trying to solve. I have a nested list of data, each populated with a list of services (identified by code).

Basically what i'm trying to achieve is the following, i want to create a recursive method that goes through every nested level, at that level it gets the codes, then checks if a child list has the same codes, whichever match then remove them from the parent.

See code example of data setup:

public class Service
        {
            public int Id { get; set; };
            public string Code { get; set; };
            public List<Service> Services { get; set; } = new List<Service>();
        }



int counter = 0;
var services = new List<Service>
{
    new Service
    {
        Id = ++counter,
        Code = "SG1",
        Services = new List<Service>
        {
            new Service {Id = ++counter, Code = "S1"},
            new Service {Id = ++counter, Code = "S2"},
            new Service {Id = ++counter, Code = "S3"},
            new Service {Id = ++counter, Code = "S4"},
            new Service {Id = ++counter, Code = "S5"},
            new Service
            {
                Id = ++counter,
                Code = "SG2",
                Services = new List<Service>
                {

                    new Service {Id = ++counter, Code = "S3"},
                    new Service {Id = ++counter, Code = "S4"},
                    new Service {Id = ++counter, Code = "S5"},
                    new Service
                    {
                        Id = ++counter,
                        Code = "SG3",
                        Services = new List<Service>
                        {
                            new Service {Id = ++counter, Code = "S3"},
                            new Service {Id = ++counter, Code = "S4"},
                        }
                    }
                }
            }
        }

    }
};

The output i'm expecting to see is the same tree but with parent items removed where they exist in a child, so something like this:

SG1

----S1

----S2

----SG2

--------S5

--------SG3

------------S3

------------S4

解决方案

Consider two new functions:

public class Service
{
	public int Id { get; set; }
	public string Code { get; set; }
	public List<Service> Services { get; set; } = new List<Service>();

	public bool Contains( string code )
	{
		return Services.Any( s => s.Code == code || s.Contains( code ) );
	}

	public void RemoveUnneeded()
	{
		Services.ForEach( s => s.RemoveUnneeded() );
		var to_remove = Services.Where( s => Services.Any( z => z != s && z.Contains( s.Code ) ) );
		to_remove.ToList().ForEach( s => Services.Remove( s ) );
	}
}


Usage:

var services = new List<Service>
. . .
services.ForEach( s => s.RemoveUnneeded() );



这篇关于在n嵌套列表中递归清理重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆