【问题标题】:Remove item in the third level of a dictionary删除字典第三级中的项目
【发布时间】:2016-04-20 13:25:10
【问题描述】:

我正在尝试删除字典第三级中的信息,并且只能在删除此信息后使用它。 但我不能,我做错了什么?

    public class Person
{
    int id;
    string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int ID
    {
        get { return id; }
        set { id = value; }
    }

   public List<Product> ListProd;
}

public class Product
{
    public int idProd;
    public string Description;
    public List<Tax> listTax;
}

public class Tax
{
    public int idTax;
    public string Value;
}

//Method
public void SomeMethod()
{
        Dictionary<int, List<int>> dicRemove = new Dictionary<int, List<int>>();
        List<int> listTaxToRemove = new List<int>();
        for (int m = 8; m < 10; m++)
        {
            listTaxToRemove.Add(m);
        }
        dicRemove.Add(10, listTaxToRemove);

        Dictionary<int, List<Person>> dic = new Dictionary<int, List<Person>>();
        List<Person> list = new List<Person>();
        for (int i = 0; i < 2; i++)
        {
            Person d = new Person();
            d.ID = i;
            d.Name = "Person " + i;
            d.ListProd = new List<Product>();
            for (int j = 3; j < 6; j++)
            {
                Product p = new Product();
                p.idProd = j;
                p.Description = "Product " + j;
                d.ListProd.Add(p);
                p.listTax = new List<Tax>();
                for (int m = 7; m < 10; m++)
                {
                    Tax t = new Tax();
                    t.idTax = m;
                    t.Value = "Tax " + m;
                    p.listTax.Add(t);
                }
            }
            list.Add(d);
        }

        dic.Add(10, list);

        var q = dic.Select(s => s.Value
                         .Select(s1 => s1.ListProd
                              .Select(s2 => s2.listTax
                         .RemoveAll(r =>!dicRemove[s.Key].Contains(r.idTax))))).ToList();

}

我尝试了很多方法,通过迭代,这种方法只是删除了不必要的记录。

谢谢!

【问题讨论】:

  • 总是添加编程语言标签!
  • @KnD182,你能对我的回答留下反馈吗?它能解决你的问题吗?

标签: c# linq dictionary


【解决方案1】:

由于Select 的延迟执行,RemoveAll 未被调用

你有 3 个Select,只有一个ToList()

q 的类型为 System.Collections.Generic.List'1[System.Collections.Generic.IEnumerable'1[System.Collections.Generic.IEnumerable'1[System.Int32]]]

存在未枚举的嵌套IEnumerable 对象

这是一个可行的变体但绝对不可读,不要这样做

var q = dic.Select(s => s.Value.Select(s1 => s1.ListProd.Select(s2 => s2.listTax.RemoveAll(r=>!dicRemove[s.Key].Contains(r.idTax)))
                                                        .ToList())
                               .ToList())
        .ToList();

改进的变体

foreach(var pair in dic)
    foreach(var person in pair.Value)
        foreach(var prod in person.ListProd)            
            prod.listTax.RemoveAll(t=>!dicRemove[pair.Key].Contains(t.idTax));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2011-12-24
    • 2012-02-21
    • 2012-08-31
    • 2011-06-16
    • 2016-03-13
    相关资源
    最近更新 更多