Answered by:
Get null properties from object

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
Answers
-
The following may be what you want.
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
- Proposed as answer by Wendy ZangMicrosoft contingent staff Tuesday, September 11, 2018 7:38 AM
- Marked as answer by Ciupaz Tuesday, September 11, 2018 7:52 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
- Marked as answer by Ciupaz Tuesday, September 11, 2018 10:19 AM
All replies
-
The following may be what you want.
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
- Proposed as answer by Wendy ZangMicrosoft contingent staff Tuesday, September 11, 2018 7:38 AM
- Marked as answer by Ciupaz Tuesday, September 11, 2018 7:52 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
- Marked as answer by Ciupaz Tuesday, September 11, 2018 10:19 AM