[英]LinQ Count based on element in child class
我有一個JSON類,我曾經將對象反序列化為:-
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。
我確實嘗試在用於查詢內部全部問題的同一LinQ的后面添加.GetType()。GetProperty(“ code”),但我不知道該如何完成。
我希望任何人都能對可能的LinQ解決方案(希望是單線)類型提出建議。
謝謝。
西蒙
我認為這就是您想要的-
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.