简体   繁体   中英

Cannot implicently convert type 'string' to 'bool' JSON

Hi I need help finding a value from a Object list that I created form aJSON file:

JSON Structure:

{
    "mp": "1",
    "mpuus": [
      {
        "id": "100",
        "tpuus": [
          "000000001",
          "000000002"
        ]
      },
      {
        "id": "101",
        "tpuus": [
          "00000003"
        ]
      }
    ]
  },
  {
    "mp": "2",
    "mpuus": [
      {
        "id": "200",
        "tpuus": [
          "0000004"
        ]
      }
    ]
  },

I convert JSON to

 RootMedicationMapping object.
List<rootMedicationMapping> = JsonHandler.DeserializeJasonArrayFromFile<RootMedicationMapping>(@"JSON File PATH");

I would like to search the list of rootMedicationMapping by the tpuu value and get the mp value.

eg the value I am passing is tpuu = 000000002, I want to get back the mp value (1 in this case)

This am trying the below syntax but it isthrowing me

'Cannot implicently convert type 'string' to 'bool'

var mpId= rootMedicationMapping.Find(x => x.MPUUs.Find(y=>y.Tpuus.Find(z=>z.Equals("tpuu value"))));

The parameter of List.Find(Predicate) needs to be a Predicate which is a delegate that takes in an instance of the object you're searching and expects a bool returned specifying if the object matches. Currently, in the Predicate of your inner Find calls you're simply returning the result of another Find call, which is a string and not the expected bool , resulting in the error you're seeing.

To fix this, you just need to make sure the Predicate arguments return bool. With what you have, you can just add checks whether or not the inner Find calls return null :

var mpId = 
    rootMedicationMapping.Find(x => 
        x.MPUUs.Find(y => 
            y.Tpuus.Find(z => 
                z.Equals("tpuu value")) != null) != null)?.mp;

And you could also write this using LINQ, allowing the null checks to be handled by Any :

var mpId = 
    rootMedicationMapping.FirstOrDefault(x => 
        x.MPUUs.Any(y => 
            y.Tpuus.Any(z => 
                z.Equals("tpuu value"))))?.mp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM