1

I have got a IncidentImageModel class and keeps a record of any injury parts. Either 1 or Zero. I could use Bool for true or false but somehow it is like that.

I want to loop through each property and if property value is 1, i would like to add property name to string.

For example Head = 1, Left Hand=1 and Right Feet = 1. But rest of the body parts values are 0.

how can i get list of body parts is 1?

public class IncidentImageModel
{
    [Key]
    public int IncidentImageID { get; set; }
    public int IncidentID { get; set; }
    public int ClientID { get; set; }
    public int Head { get; set; } = 0;
    public int Neck { get; set; } = 0;

    public int RightShoulder { get; set; } = 0;
    public int LeftShoulder { get; set; } = 0;
    public int Chest { get; set; } = 0;

    public int RightArm { get; set; } = 0;
    public int LeftArm { get; set; } = 0;
    public int LowerAbdomin { get; set; } = 0;

    public int RightHand { get; set; } = 0;
    public int Genitals { get; set; } = 0;
    public int LeftHand { get; set; } = 0;

    public int LeftUperLeg { get; set; } = 0;
    public int RightUperLeg { get; set; } = 0;

    public int RightLowerLeg { get; set; } = 0;
    public int LeftLowerLeg { get; set; } = 0;

    public int RightFeet { get; set; } = 0;
    public int LeftFeet { get; set; } = 0;
}

I know i can do if() for each body part but i am sure there is a better way to do it. If anyone knows how to do it.

I tried this and get all properties but couldn't get property values.

 PropertyInfo[] properties = 
 incident.IncidentImageModels.GetType().GetProperties();
 for(int i=0; i<properties.count();i++)
 { 
    properties[i].Name
 }
AliAzra
  • 889
  • 1
  • 9
  • 28
  • Possible duplicate of [How do you get the Value of a property from PropertyInfo?](https://stackoverflow.com/questions/26382810/how-do-you-get-the-value-of-a-property-from-propertyinfo) – asathkum Aug 22 '19 at 12:45

3 Answers3

2

You can use the GetValue method in combination with LINQ if you only want the properties and values with value 1:

var incidentImageModel = new IncidentImageModel();
PropertyInfo[] properties = incidentImageModel.GetType().GetProperties();

var result = from property in properties
             let nameAndValue = new { property.Name, Value = (int)property.GetValue(incidentImageModel) }
             where nameAndValue.Value == 1
             select nameAndValue;
vdL
  • 263
  • 1
  • 8
  • Hi vdL. I changed my Model to each body part boolean type. But now your code doesn't work. Unable to cast object of type 'System.Int32' to type 'System.Boolean'. Because IncidentID is int type but Head,shoulder etc. Boolean. Do you know how to fix this issue now. – AliAzra Aug 22 '19 at 13:57
  • You can just replace the cast `(int)` with `(bool)` and in the where condition replace `1` with `true`. The `PropertyInfo.GetValue` method returns an object so you have to cast it to an int or bool if you want to compare it to an int or bool. – vdL Aug 22 '19 at 16:58
  • Thank you vdL. I did exactly what you said but same error. I think int==1 and bool ==true aren't same for the compailer. I am not sure. I used Tolbxela answer and with if condition, the code seems working now. Thank you for help – AliAzra Aug 23 '19 at 08:54
2

Here is your fixed code:

PropertyInfo[] properties = incident.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
    var pName = properties[i].Name;
    var pValue = properties[i].GetValue(incident);
    Console.WriteLine($"{pName} = {pValue}");
}
Tolbxela
  • 4,767
  • 3
  • 21
  • 42
1

You can use PropertyInfo.GetValue method:

https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.getvalue?view=netframework-4.8

Ömer Baş
  • 98
  • 1
  • 7