0

I need to retrieve the following JSON from my CosmosDb, but I think the C# code (which works for a simple JSON structure), needs help. The response just shows

{ RolePermission[0]  } 



var response = this.client.CreateDocumentQuery<RolePermission>(
                       UriFactory.CreateDocumentCollectionUri("demo", "RoleMenus"), queryOptions)
                       .Where(f => f.RoleName == roleName).AsEnumerable().ToArray();

and the JSON is:

{
"id": "1",
"RoleName": "Admin",
"Menus": [
    {
        "Item": "Admin",
        "Display": "Admin",
        "Granted": true
    },
    {
        "Item": "Users",
        "Display": "Users",
        "Granted": true
    }
],
}

The Class defn, I want to get it into is:

public class RolePermission
{
    [DisplayName("Role Name")]
    public string RoleName { get; set; }
    public List<Menus> Menus { get; set; }
}

public class Menus
{
    public string Item { get; set; }
    public string Display { get; set; }
    public bool Granted { get; set; }
}

I just need the menus part of the JSON

Thanks

8
  • may be you need to cast it? Commented Feb 22, 2022 at 15:50
  • What is the structure of your C# class? Commented Feb 22, 2022 at 15:56
  • Ok, just realized I did'nt put in the class, but have now, in the question Commented Feb 22, 2022 at 16:05
  • this.client.CreateDocumentQuery<List<RolePermission>>? Commented Feb 22, 2022 at 16:07
  • @Jay , then the f.RoleName fails as List<RolePermission> does not contain a defn for RoleName Commented Feb 22, 2022 at 16:16

1 Answer 1

1

According to the data that you provided and the code , it should return the list you need "Menus" with the below query

var response = this.client.CreateDocumentQuery<RolePermission>(UriFactory.CreateDocumentCollectionUri("demo", "RoleMenus"), queryOptions).Where(f => f.RoleName == roleName).ToList(); 

Make sure you have the correct data inserted with the field RoleName in the container.

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

Comments

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.