1

I have the below model and I want to access the Name and Type from the class Property:

 public partial class RootObject
    {
        [JsonProperty("edmx:Edmx")]
        public EdmxEdmx EdmxEdmx { get; set; }
    }

    public partial class EdmxEdmx
    {
        [JsonProperty("EntityType")]
        public List<EntityType> EntityType { get; set; }
    }

    public partial class EntityType
    {
        [JsonProperty("-Name")]
        public string Name { get; set; }

        [JsonProperty("Property")]
        public List<Property> Property { get; set; }
    }

    public partial class Property
    {
        [JsonProperty("-Name")]
        public string Name { get; set; }

        [JsonProperty("-Type")]
        public String Type { get; set; }

    }

I have done the below:

 var r = JsonConvert.DeserializeObject<RootObject>(o1.ToString());

How do I write a loop that accesses name and type ?

Edit: Here is my JSON:

{
  "edmx:Edmx": {
    "-xmlns:edmx": "http://docs.oasis-open.org/odata/ns/edmx",
    "-Version": "4.0",
    "EntityType": [
      {
        "-BaseType": "mscrm.crmbaseentity",
        "-Name": "EntityA",
        "Property": [
          {
            "-Name": "address2_line1",
            "-Unicode": "false",
            "-Type": "Edm.String"
          },
          {
            "-Name": "territorycode",
            "-Type": "Edm.Int32"
          },
          {
            "-Name": "EntityID",
            "-Type": "Edm.Guid"
          },
          {
            "-Name": "address1_telephone1",
            "-Unicode": "false",
            "-Type": "Edm.String"
          }
        ]
      }
}
}

The above JSON is the o1.ToString. I use a JObject to create a JSON object from a text file.

EDIT2:

Let me try to be more precise: I want to get this per entity: what I want

10
  • sorry, added the json Commented Jul 20, 2018 at 16:33
  • Okay, so you've got that much... it's not clear where you're stuck. Note that there can be multiple entity types and multiple properties per entity type... what are you trying to do with those, and what's happening at the moment? Commented Jul 20, 2018 at 16:36
  • It is not failing. I just want to access Name and Type in the class Property. Commented Jul 20, 2018 at 16:38
  • I can do this string name = r.EdmxEdmx.EntityType[0].Name. This gives me the name of the Entity. Then I need to access the Name and Type of that entity. Commented Jul 20, 2018 at 16:41
  • It's still not clear what you mean. The only Type property is within Property, and there are multiple properties. What value would you expect to get from the JSON you've shown? Commented Jul 20, 2018 at 16:45

1 Answer 1

1

I'm not sure what is hanging you up on this. You have all the pieces, all you need is a nested foreach loop.

foreach (var entityType in r.EdmxEdmx.EntityType)
{
    foreach (var property in entityType.Property)
    {
        Console.WriteLine("-Name: " + property.Name);
        Console.WriteLine("-Type: " + property.Type);
        Console.WriteLine();
    }
}

Fiddle: https://dotnetfiddle.net/33n7TO

Sign up to request clarification or add additional context in comments.

1 Comment

My problem was that I didn't know I had to use a nested loop. Thanks Brian.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.