2014-10-22 61 views
-3

我這是越來越充滿正確的,但之後其填充我想刪除所有的按鍵與一個簡單的方法爲空值,如果存在一個列表,像:從列表中刪除空值<classname>

posts.RemoveAll(item => item == null);

我在這個列表中有多個職位<> 對於我來說,問題是要找到鍵/值來訪問字典。

有人知道如何做到這一點嗎?

UPDATE:

我的課是這樣的:

public class iPost 
{ 
    public int id { get; set; } 
    public int post_origin_post_id { get; set; } 
    public int pid { get; set; } 
    public int container_type_id { get; set; } 
    public int post_member_id { get; set; } 
    public int post_toGroup_id { get; set; } 
    public string title { get; set; } 
    public string comment { get; set; } 
    public string latitude { get; set; } 
    public string longitude { get; set; } 
    public string post_ip { get; set; } 
    public bool canShared { get; set; } 
    public bool isShared { get; set; } 
    public int share_type_id { get; set; } 
    public int views { get; set; } 
+3

''我有一個List ...我想刪除所有具有空值的鍵「'。 '列表'沒有鍵/值,只有'T's。如果你需要任何有用的幫助,你將需要展示一些示例代碼(一個最小的工作示例)。 ['List .RemoveAll'](http://msdn.microsoft.com/zh-cn/library/wdka673a.aspx)接受一個謂詞,您可以根據需要選擇要刪除的對象。 – 2014-10-22 03:26:06

+0

這聽起來像你想'posts.RemoveAll(item => item.key == null);'?但是你需要顯示你的classname類。 – 2014-10-22 03:28:59

+0

http://stackoverflow.com/questions/1636885/remove-item-in-dictionary-based-on-value – 2014-10-22 05:47:25

回答

0

哪裏是你的鑰匙嗎?一個c#-List沒有鍵。

你的意思somethong這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

    namespace DemoApp 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       List<DemoClass> list = new List<DemoClass>(); 

       list.Add(new DemoClass() { Key = "A1" }); 
       list.Add(new DemoClass() { Key = "A2" }); 
       list.Add(new DemoClass() { Key = "A3" }); 

       // Print 
       foreach (var it in list) { Console.WriteLine(it.Key); } 
       Console.WriteLine(); 

       // set one key to null 
       list[1].Key = null; 

       list.RemoveAll(Item => Item.IsToDelete()); 

       // Print 
       foreach (var it in list) { Console.WriteLine(it.Key); } 
       Console.WriteLine("As you can see, A2 is missing"); 

       Console.ReadLine(); 
      } 
     } 

     class DemoClass 
     { 
      public string Key 
      { 
       get; 
       set; 
      } 

      public object SomeValue 
      { 
       get; 
       set; 
      } 

      public bool IsToDelete() 
      { 
       if(Key == null || SomeValue == null) 
       { 
        return true; 
       } 

       return false; 
      } 
     } 
    } 

編輯 可以編寫自己的屬性,這是擺在誰不allwed爲空的所有成員。你可以編寫一個讀取這些信息的類,然後決定是否將你的實例從列表中移除。 Absract例如:

[ProofForNull] 
public string Key 
{ 
    get; 
    set; 
} 

[ProofForNull] 
public string Value1 
{ 
    get; 
    set; 
} 

[ProofForNull] 
public string Value2 
{ 
    get; 
    set; 
} 

比你改變IsToDelete()。如果它們具有[ProofForNull]屬性,它必須通過反射和證明來獲取所有成員。如果具有該屬性的一個成員爲空,則IsToDelete()返回true。 使用這種方法,您有一項最初的工作要做,但是您的類的實現要容易得多。

+0

作爲例子,但與任何對象 – user3763117 2014-10-22 08:13:20

+0

@ user3763117我不明白你的意思,** ID * *是你的鑰匙?問題是什麼? – BendEg 2014-10-22 08:46:41

+0

它是行鍵是的,但我希望它遍歷所有對象,並檢查對象是否有「空」值並刪除此對象。所以在你的例子{Key = 1,description =「something」,subject = null}我想要它刪除「subject」 – user3763117 2014-10-22 11:17:11