检查对象列表,所有属性包含null,然后从c#中删除。

时间:2022-09-25 10:24:17

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.

我有在类对象列表中反序列化的json数据列表,但如果proerties名称与json数据不匹配,则取值null。

检查对象列表,所有属性包含null,然后从c#中删除。 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:

Json数据来自于url,因为用户依赖用户可以输入任何数据,那么当属性不匹配时,我将为验证处理null。sampleJson数据列表如下:

[{"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.

我的问题是,如果对象列表中所有属性都包含空值,那么从列表中删除。

2 个解决方案

#1


1  

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

要在这个特定的情况下进行过滤,你只需应用一些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.

如果需要对一组已知类型进行此操作,则重载NotAllFieldsNull。如果需要为任何(引用)类型工作,您将需要反射。

#2


0  

While I'm sure you could reflect through the properties, I would add another property to the model that checks if there are any values.

虽然我确信您可以通过属性进行反射,但我将向模型添加另一个属性,以检查是否存在任何值。

e.g.

如。

public bool HasValues
{
    get
    {
        return !string.IsNullOrWhiteSpace(this.AdsTitle) ||
               !string.IsNullOrWhiteSpace(this.Description) ||
               this.ToDateTime.HasValue ||
               ... etc ...
    }
}

Then when I have my list, I would just remove them with Linq like:

然后当我有我的列表时,我会用Linq删除它们,比如:

adsImportEntityList = adsImportEntityList.Where((e) => e.HasValues).ToList();

#1


1  

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

要在这个特定的情况下进行过滤,你只需应用一些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.

如果需要对一组已知类型进行此操作,则重载NotAllFieldsNull。如果需要为任何(引用)类型工作,您将需要反射。

#2


0  

While I'm sure you could reflect through the properties, I would add another property to the model that checks if there are any values.

虽然我确信您可以通过属性进行反射,但我将向模型添加另一个属性,以检查是否存在任何值。

e.g.

如。

public bool HasValues
{
    get
    {
        return !string.IsNullOrWhiteSpace(this.AdsTitle) ||
               !string.IsNullOrWhiteSpace(this.Description) ||
               this.ToDateTime.HasValue ||
               ... etc ...
    }
}

Then when I have my list, I would just remove them with Linq like:

然后当我有我的列表时,我会用Linq删除它们,比如:

adsImportEntityList = adsImportEntityList.Where((e) => e.HasValues).ToList();