简体   繁体   English

从 c# 中的多级集合中删除项目

[英]Remove item from multilevel collection in c#

I have a multilevel collection such as我有一个多级集合,例如

public class Year
{
    public IList<Month> months { get; set; }
}

public class Month
{
    public IList<Day> days { get; set; }
}
public class Day
{
    public IList<Hour> hour { get; set; }
}
public class Hour
{
    public bool IsWorkinghour { get; set; }
    // some other properties here   
}

Here Year contain a list of months, and months contain a list of day, same days contain a list of hours, its a 4 level collection.这里 Year 包含一个月份列表,months 包含一个 day 列表,same days 包含一个小时列表,它是一个 4 级集合。 In an object of type "Year", I want to remove hours where Isworkinghour is false, and also if all hours are set as false under a day than remove that day also.在“年”类型的 object 中,我想删除 Isworkinghour 为 false 的时间,并且如果一天中的所有时间都设置为 false,那么也删除那一天。 Below is my code for that.下面是我的代码。

var year = new Year();
        if(year != null && year.months.Count() > 0)
        {
            foreach(var m in year.months.ToList())
            {
                foreach(var d in m.days.ToList())
                {
                    foreach(var h in d.hour.ToList())
                    {
                        if(!h.IsWorkinghour)
                        {
                            d.hour.Remove(h);
                        }
                    }
                    if(d.hour.Count == 0)
                    {
                        m.days.Remove(d);
                    }
                }
            }
        }

Can someone please let me know where is the mistake in this as it does not remove the items, Thanks.有人可以让我知道这里的错误在哪里,因为它没有删除项目,谢谢。

If you don't want to use .ToList() ,you can put new data to new Year():如果你不想使用.ToList() ,你可以把新的数据放到 new Year() 中:

var newyear= new Year();
if (year != null && year.months.Count() > 0)
{
    List<Month> months = new List<Month>();
    foreach (var m in year.months)
    {
        List<Day> days = new List<Day>();
        foreach (var d in m.days)
        {
            List<Hour> hours = new List<Hour>();
            foreach (var h in d.hour)
            {
                if (h.IsWorkinghour)
                {
                    hours.Add(h);
                }
            }
            if (hours.Count() > 0)
            {
                days.Add(new Day { hour = hours });
            }
        }
        months.Add(new Month { days = days });
    }
    newyear.months = months;
}

Your code looks fine from a logical view.从逻辑角度看,您的代码看起来不错。 You could use lambda expression to compress your code if you want to.如果你愿意,你可以使用 lambda 表达式来压缩你的代码。

// remove all hours where IsWorkinghour is true
year.months.ToList().ForEach(x => x.days.ToList().ForEach(y => y.hour.ToList().RemoveAll(z => z.IsWorkinghour)));
    
// remove all days that doesn't contain any hours
year.months.ToList().ForEach(x => x.days.ToList().RemoveAll(y => y.hour.Count == 0));

This lambda expression expects that all lists in the Year Object are at least initialized.这个 lambda 表达式期望Year Object 中的所有列表至少被初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM