Home > front end >  Extracting a value from an object from a dapper query in backend
Extracting a value from an object from a dapper query in backend

Time:02-27

I am getting an array of objects(oMessageData) from my backend like this:

foreach(var l in oMessageData)
{
                                        
     Console.WriteLine(JsonConvert.SerializeObject(l));
}

and one of the result is:

{some data}
{some data}
{some data}
{some data}
{"SetId":"1","ValueType":"FT","ObservationIdentifier":"COVID-19 qRT-PCR","ObservationValue":"Not Detected","ObservationResultStatus":"F","DateAndTimeOfObservation":"2022-02-02T22:53:26.28"}
{some data}

I want to extract DateAndTimeOfObservation , but whenn I do var date = oMessageData[4].['DateAndTimeOfObservation'], it says Identifier expected. I also couldn't get it with : oMessageData[4].values[5].

How do I extract that value?

This is the structure of 0MessageData[4]: oMessageData[4]

Edit: I am getting this data from dapper query and the first element in object is DapperRow,like this:

{{DapperRow, SetId = '1', ValueType = 'FT', ObservationIdentifier = 'COVID-19 qRT-PCR', ObservationValue = 'Not Detected', ObservationResultStatus = 'F', DateAndTimeOfObservation = '02-02-2022 22:53:26'}}

CodePudding user response:

According to the screenshot you get a dicionary object. This means that the target record is not lookable by an index, like in an array, but by a key.

On the screenshot the Values property is expanded. Each of these entries is assigned to a key from the Keys property. With this key you can access the target entry.

For more details on dictionaries, see https://docs.microsoft.com/de-de/dotnet/api/system.collections.generic.idictionary-2?view=net-6.0

CodePudding user response:

As Gabriel is pointing out, the variable displayed in the screenshot seems to be of a dictionary type. I suspect that if you expand the System.Collections.Generic.IDictionary<string, object>.Keys line (the line above the first expanded line in your screenshot), you will see "DateAndTimeOfObservation" being listed as one of the dictionary keys.

You claim that the screenshot displays the structure of oMessageData[4]; in that case, you should get the desired value by setting date as follows:

var date = oMessageData[4]["DateAndTimeOfObservation"];

Or, in a more descriptive manner; displaying more clearly how you obtain the value for a given key in a dictionary:

var dictionary = oMessageData[4];
var key = "DateAndTimeOfObservation";
var date = dictionary[key];
  • Related