Home > Software design >  Extracting value from a list of object[]
Extracting value from a list of object[]

Time:01-06

I am working with a Firebird database, and the following code extracts some data:

FbDataReader reader = readCommand.ExecuteReader();

var rows = new List<object>();
while (reader.Read())
      {
       var columns = new object[reader.FieldCount];
       reader.GetValues(columns);
       rows.Add(columns);
      }

Now that I have my data read to "rows", it looks like this:

enter image description here

"ЛР КЛ1 220кВ" is the string value I need to extract. The following code is supposed to do it:

var nameOfProperty = "property_name";
var propertyInfo = rows[1].GetType().GetProperty(nameOfProperty);
var value = propertyInfo.GetValue(rows[1], null);

I get System.NullReferenceException at the last line, because "property_name" must contain a real property name. How do I get correct property name?

CodePudding user response:

You have list inside list.

Try :

...
var value = propertyInfo.GetValue(rows[1][0], null);
...
  •  Tags:  
  • Related