json - Check List of object all properties Contains null then remove from list in c# -


i have list of json data have deserialize in list of class object if proerties name not match json data takes value null.

enter image description here json data come url user dependent user can enter data validation handle null when properties not match. samplejson list of data like:

[{"adsname":"francis","adsimageurl":"andrew love.jpg","ontop":false,"key":30012647,"onscan":true,"adscode":6689390,"brandname":{"adsbrand":"beth moon"},"category":"new ads","adsscription":"weinstein jacob sutton","from":"2016-12-30t00:00:00","to":"2016-12-30t00:00:00"},{"adsname":"mckay","adsimageurl":"lorraine spencer.jpg","ontop":false,"key":136301519,"onscan":true,"adscode":346146503,"brandname":{"adsbrand":"russell warner"},"category":"new ads","adsscription":"stanton thomas moran","from":"2016-12-30t00:00:00","to":"2016-12-30t00:00:00"},{"adsname":"berger","adsimageurl":"lois norton.jpg","ontop":false,"key":32971839,"onscan":false,"adscode":334075948,"brandname":{"adsbrand":"becky park"},"category":"new ads","adsscription":"gallagher matthew pitts","from":"2016-12-30t00:00:00","to":"2016-12-30t00:00:00"},{"adsname":"boswell","adsimageurl":"constance scarborough.jpg","ontop":false,"key":183877654,"onscan":true,"adscode":230154009,"brandname":{"adsbrand":"yvonne hardy"},"category":"new ads","adsscription":"riddle nancy atkins","from":"2016-12-30t00:00:00","to":"2016-12-30t00:00:00"}] 

my model propeties class

public class adsimportentity {     [jsonproperty(propertyname = "title")]     public string adstitle { get; set; }      [jsonproperty(propertyname = "description")]     public string description { get; set; }      [jsonproperty(propertyname = "barcode")]     public string barcode { get; set; }      [jsonproperty(propertyname = "top")]     public bool? top { get; set; }      [jsonproperty(propertyname = "fromdatetime")]     public system.datetime? fromdatetime { get; set; }      [jsonproperty(propertyname = "todatetime")]     public datetime? todatetime { get; set; }      [jsonproperty(propertyname = "httpimageurl")]     public string httpimageurl { get; set; } } 

my question if in list of object if object properties contain null value remove list.

to filter in specific case apply little linq:

adsimportentitylist = converter.deserialize<list<adsimportentity>>(adsjson)                                .where(x => !(x.adstitle == null                                              && x.description == null                                              && ...))                                .tolist(); 

if needed in multiple places filter use help:

static bool notallfieldsnull(adsimportentity x) {   return !(x.adstitle == null            && x.description == null            && ...); }  adsimportentitylist = converter.deserialize<list<adsimportentity>>(adsjson)                                .where(notallfieldsnull)                                .tolist(); 

if needed know set of types overload notallfieldsnull. if needs work (reference) type you'll need reflection.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -