none
Get null properties from object RRS feed

  • Question

  • Hello,
    I have an object that has an hundred of properties. 

    I need to write a method that, having an instance of this obiect, get me the names 
    of the fields that have no values (null). 

    At the moment I have something like:

    string result = "";

    if (myObj.field1 == null)
    {
        result += "field1 is null";
    }
    ...
    ...
    if (myObj.field100 == null)
    {
        result += "field100 is null";
    }



    Is there another better way to accomplish this?

    Thanks a lot in advance. 


    Luigi
    Monday, September 10, 2018 6:29 PM

Answers

  • The following may be what you want.

    https://stackoverflow.com/questions/6916155/reflection-check-for-null-or-empty-for-each-property-field-in-a-class

    List<string> values
        = typeof(FilterParams).GetProperties()
                              .Select(prop => prop.GetValue(yourObject, null))
                              .Where(val => val != null)
                              .Select(val => val.ToString())
                              .Where(str => str.Length > 0)
                              .ToList();

    .

    public class FilterParams
    {
        public string MeetingId { get; set; }
        public int? ClientId { get; set; }
        public string CustNum { get; set; }
        public int AttendedAsFavor { get; set; }
        public int Rating { get; set; }
        public string Comments { get; set; }
        public int Delete { get; set; }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, September 10, 2018 6:37 PM
  • Yes because I didn't properly account for not null and the .ToString(). Try the following. In HasNullValue the types are string, int32 and DateTime, add others as needed.

    public class FilterParams
    {
        public string MeetingId { get; set; }
        public int? ClientId { get; set; }
        public string CustNum { get; set; }
        public int AttendedAsFavor { get; set; }
        public int Rating { get; set; }
        public string Comments { get; set; }
        public int Delete { get; set; }
        public DateTime MeetingDate { get; set; }
    }

    This is for checking for null

    private bool HasNullValue(FilterParams sender, PropertyInfo pi)
    {
        if (pi == null)
        {
            return true;
        }
    
        if (pi.GetValue(sender, null) == null)
        {
            return true;
        }
    
        var propType = pi.PropertyType;
        var value = pi.GetValue(sender, null);
        bool result = false;
    
        switch (propType.Name)
        {
            case "Int32":
                if ((int) value == 0 || (int) value == -1)
                {
                    result = true;
                }
                break;
            case "DateTime":
                if ((DateTime)value == default(DateTime))
                {
                    result = true;
                }
                break;
        }
        return result;
    }

    Example

    var yourObject = new FilterParams() { AttendedAsFavor = 1, CustNum = "A", MeetingDate = DateTime.Now.AddDays(1)};
    var values  = typeof(FilterParams).GetProperties().ToList();
    var nullItems = new List<string>();
    
    foreach (var item in values)
    {
        if (HasNullValue(yourObject,item))
        {
            nullItems.Add(item.Name);
        }
    }
    
    if (nullItems.Count >0)
    {
        nullItems.ForEach(Console.WriteLine);
    }

    Results

    MeetingId
    ClientId
    Rating
    Comments
    Delete
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ciupaz Tuesday, September 11, 2018 10:19 AM
    Tuesday, September 11, 2018 10:17 AM

All replies

  • The following may be what you want.

    https://stackoverflow.com/questions/6916155/reflection-check-for-null-or-empty-for-each-property-field-in-a-class

    List<string> values
        = typeof(FilterParams).GetProperties()
                              .Select(prop => prop.GetValue(yourObject, null))
                              .Where(val => val != null)
                              .Select(val => val.ToString())
                              .Where(str => str.Length > 0)
                              .ToList();

    .

    public class FilterParams
    {
        public string MeetingId { get; set; }
        public int? ClientId { get; set; }
        public string CustNum { get; set; }
        public int AttendedAsFavor { get; set; }
        public int Rating { get; set; }
        public string Comments { get; set; }
        public int Delete { get; set; }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, September 10, 2018 6:37 PM
  • This give me the "good" values, but I need only the "bad" values (i.e. those that are null). 

    But if I write:

    .Where(val => val == null)

    give me an exception. 

    Luigi

    Tuesday, September 11, 2018 7:32 AM
  • Yes because I didn't properly account for not null and the .ToString(). Try the following. In HasNullValue the types are string, int32 and DateTime, add others as needed.

    public class FilterParams
    {
        public string MeetingId { get; set; }
        public int? ClientId { get; set; }
        public string CustNum { get; set; }
        public int AttendedAsFavor { get; set; }
        public int Rating { get; set; }
        public string Comments { get; set; }
        public int Delete { get; set; }
        public DateTime MeetingDate { get; set; }
    }

    This is for checking for null

    private bool HasNullValue(FilterParams sender, PropertyInfo pi)
    {
        if (pi == null)
        {
            return true;
        }
    
        if (pi.GetValue(sender, null) == null)
        {
            return true;
        }
    
        var propType = pi.PropertyType;
        var value = pi.GetValue(sender, null);
        bool result = false;
    
        switch (propType.Name)
        {
            case "Int32":
                if ((int) value == 0 || (int) value == -1)
                {
                    result = true;
                }
                break;
            case "DateTime":
                if ((DateTime)value == default(DateTime))
                {
                    result = true;
                }
                break;
        }
        return result;
    }

    Example

    var yourObject = new FilterParams() { AttendedAsFavor = 1, CustNum = "A", MeetingDate = DateTime.Now.AddDays(1)};
    var values  = typeof(FilterParams).GetProperties().ToList();
    var nullItems = new List<string>();
    
    foreach (var item in values)
    {
        if (HasNullValue(yourObject,item))
        {
            nullItems.Add(item.Name);
        }
    }
    
    if (nullItems.Count >0)
    {
        nullItems.ForEach(Console.WriteLine);
    }

    Results

    MeetingId
    ClientId
    Rating
    Comments
    Delete
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ciupaz Tuesday, September 11, 2018 10:19 AM
    Tuesday, September 11, 2018 10:17 AM