Ljdhameliya Ljdhameliya - 27 days ago 12
JSON Question

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

I have list of json data i have deserialize in list of class object but if proerties name does not match with json data it takes value null.

enter image description here
Json data come from the url as user dependent user can enter any data then what i will do for validation to handle null when properties not match.
sampleJson list of data is 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 like

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 is If In list of object if object all properties contain null value then remove from list.

Answer

To filter in this specific case you could simply apply a little LINQ:

adsImportEntityList = Converter.Deserialize<List<AdsImportEntity>>(adsJson)
                               .Where(x => !(x.AdsTitle == null
                                             && x.Description == null
                                             && ...))
                               .ToList();

If needed in multiple places the filter could use a help:

static bool NotAllFieldsNull(AdsImportEntity x) {
  return !(x.AdsTitle == null
           && x.Description == null
           && ...);
}

adsImportEntityList = Converter.Deserialize<List<AdsImportEntity>>(adsJson)
                               .Where(NotAllFieldsNull)
                               .ToList();

If this is needed for a know set of types then overload NotAllFieldsNull. If it needs to work for any (reference) type you'll need reflection.