2015-02-09 31 views
0

我有一個JSON類,我用我的對象序列化到: -LinQ在基於計數元素在子類

public class Response 
    { 
     private Meta _meta; 
     private Result _result; 
     private Output _output; 
     public Meta meta 
     { 
      set 
      { 
       if (this._meta == null) 
       { 
        this._meta = new Meta(); 
       } 

       this._meta = value; 
      } 

      get 
      { 
       return this._meta; 
      } 
     } 
     public Output output 
     { 
      set 
      { 
       if (this._output == null) 
       { 
        this._output = new Output(); 
       } 
       this._output = value; 
      } 
      get 
      { 
       return this._output; 
      } 
     } 
    } 

哪個繼承

public class Output 
    { 

      ... 

      public Verified verified{ 

      get 
      { 
       return this._verified; 
      } 
      set 
      { 
       if (this._verified == null) 
       { 
        this._verified = new Verified(); 
       } 
       this._verified = value; 
      } 

     } 

在有子類的

public class Verified 
    { 
... 

public Address Address 
     { 

      set 
      { 
       if (this.address == null) 
       { 
        this.address = new Address(); 
       } 
       this.address = value; 
      } 
      get 
      { 
       return this.address; 
      } 
     } 
     public Age Age 
     { 
      get 
      { 
       return this.age; 
      } 
      set 
      { 
       if (this.age == null) 
       { 
        this.age = new Age(); 
       } 
       this.age = value; 
      } 
     } 
     public City City 
     { 
      get 
      { 
       return this.city; 
      } 
      set 
      { 
       if (this.city == null) 
       { 
        this.city = new City(); 
       } 
       this.city = value; 
      } 
     } 

... 

城市,年齡和地址中的所有屬性都是相同的,如

public class Address 
    { 
     public int code { get; set; } 
     public string text { get; set; } 
    } 

我已經設法計算有多少屬性在驗證通過

TotalQuestion = response.output.verified.GetType().GetProperties() 
        .Where(p => !p.PropertyType.IsGenericType 
           && !p.PropertyType.IsArray) 
        .Count(); 

,而這僅僅是我所關注的一半。我還必須計算地址,城市,年齡中的每個類中的許多屬性「代碼」,其值爲3.

我曾嘗試添加.GetType().GetProperty(「code」)在同一個LinQ的後面,我用來查詢內部問題的總量,但我迷失在腦海中如何完成它。

我希望任何人都能夠就可能的LinQ解決方案(希望單線)類型提供建議。

謝謝。

西蒙

回答

1

我認爲這是你在找什麼 -

var result = resp.output.verified.GetType().GetProperties().Where( 
     child => { 
        var prop = child.GetValue(resp.output.verified, null); 
        return (int)prop.GetType().GetProperty("code").GetValue(prop, null) == 3; 
       }).ToList();