2015-02-12 116 views
1

讓我們假設簡單的數據:選擇行>>

Dictionary<string, object> dict = new Dictionary<string, object>(); 
dict.Add("obj 1", null); 
dict.Add("obj 2", new object()); 
dict.Add("obj 3", new object()); 
dict.Add("obj 4", null); 

Dictionary<string, object> dict2 = new Dictionary<string, object>(); 
dict2.Add("obj 1", null); 
dict2.Add("obj 2", new object()); 
dict2.Add("obj 3", null); 
dict2.Add("obj 4", null); 

Dictionary<string, object> dict3 = new Dictionary<string, object>(); 
dict3.Add("obj 1", null); 
dict3.Add("obj 2", null); 
dict3.Add("obj 3", new object()); 
dict3.Add("obj 4", null); 

List<Dictionary<string, object>> list = new List<Dictionary<string, object>> {dict, dict2, dict3}; 

是否有可能選擇行和刪除鍵/值,其中的所有字段爲空?所以,在這之後我的列表將是:

dict({"obj 2", object}, {"obj 3", object}) 
dict2({"obj 2", object}, {"obj 3", null}) 
dict2({"obj 2", null}, {"obj 3", object}) 
+0

http://stackoverflow.com/questions/1636885/remove-item-in-dictionary-based-on-value – andy 2015-02-12 07:16:14

回答

1

試試這個:

list.ForEach(x => 
     { 

      x.Where(y => y.Value == null).ToList().ForEach(z=> 
       { 
        if(list.All(l=>!l.ContainsKey(z.Key)|| l[z.Key]==null)) 
         x.Remove(z.Key); 
       }); 
     }); 
+0

Thx,完美的作品:) – Smitis 2015-02-12 08:33:22

+0

很高興我幫你,請upvote我的回答 – 2015-02-12 08:38:55

+0

我想但我需要2更多的聲譽..當我達到它,我會做到這一點 – Smitis 2015-02-12 09:03:54

相關問題