2016-04-20 42 views
0

我試圖刪除字典第三級中的信息,並且只能在刪除此信息後使用它。 但我不能,我做錯了什麼?在字典的第三級刪除項目

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(); 

} 

我嘗試了很多方法,通過迭代,這種方法剛剛刪除了不必要的記錄。

謝謝!

+0

務必添加一種編程語言的標籤! –

+0

謝謝!編輯 – KnD182

+0

@ KnD182,請您對我的答案留下反饋意見嗎?它解決你的問題嗎? – ASh

回答

0

RemoveAll不調用由於Select

延遲執行你有3個Select且只有一個ToList()

q已經鍵入哪些未列舉System.Collections.Generic.List'1[System.Collections.Generic.IEnumerable'1[System.Collections.Generic.IEnumerable'1[System.Int32]]]

存在嵌套IEnumerable對象

這裏是一個工作變體但是一個bsolutely不可讀,不要做這樣的

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));