0

So here is my JSON:

[
  {
    "Assignees": [
      {
        "ID": "1111",
        "IsPrimaryOffice": true
      },
      {
        "ID": "2222",
        "IsPrimaryOffice": false
      }
    ],
    "Height": "76",
    "Width": "78",
    "Top": "160",
    "Left": "99.5"
  },
  {
    "Assignees": [
      {
        "ID": "3333",
        "IsPrimaryOffice": true
      },
      {
        "ID": "4444",
        "IsPrimaryOffice": false
      }
    ],
    "Height": "11",
    "Width": "11",
    "Top": "11",
    "Left": "11"
  },
  {
    "Assignees": null,
    "Height": "22",
    "Width": "22",
    "Top": "22",
    "Left": "22"
  },
]

Where each main object in my array holds an array of sub-objects "Assignees".

So what I'm trying to do is to search each "Assignees" object in its array for a match on ID.

For example: I want to find the Assignee object which has an ID of "3333" and also has a true value for IsPrimaryOffice with LINQ. How can I do that? Here is what I came up with, but it always returns null:

mainObject.Where(x => 
                 x.Assignees != null &&
                 x.Assignees.Any(y => y.ID == "3333" && y.IsPrimaryOffice == true))
          .FirstOrDefault();

Can anyone help me out here? Thank you in advance

2
  • 1
    have you deserialized the JSON into proper objects? Commented Dec 22, 2015 at 17:39
  • What are you trying to find? The assignee object (item in the inner Assignees array) or item from outer array which contains Assignee object with specified parameters in its Assignees array? Commented Dec 22, 2015 at 18:03

1 Answer 1

3

I have created class using json2sharp for provided json :

var jsonArray = "[{\"Assignees\":[{\"ID\": \"1111\",\"IsPrimaryOffice\": true      },
  {\"ID\": \"2222\",\"IsPrimaryOffice\": false      }    ],
\"Height\": \"76\",    \"Width\": \"78\",    \"Top\": \"160\",    \"Left\": \"99.5\"  },  
{    \"Assignees\": [      {\"ID\": \"3333\",\"IsPrimaryOffice\": true      },     
{\"ID\": \"4444\",\"IsPrimaryOffice\": false      }    ],
\"Height\": \"11\",    \"Width\": \"11\",    \"Top\": \"11\",    \"Left\": \"11\"  }]";

which generated as :

public class Assignee
{
    public string ID { get; set; }
    public bool IsPrimaryOffice { get; set; }
}

public class RootObject
{
    public List<Assignee> Assignees { get; set; }
    public string Height { get; set; }
    public string Width { get; set; }
    public string Top { get; set; }
    public string Left { get; set; }
}

Now when I ran the below query

var rootObj = JsonConvert.DeserializeObject<JArray>(jsonArray).ToObject<List<RootObject>>().Where(x => 
                 x.Assignees != null &&
                 x.Assignees.Any(y => y.ID == "3333" && y.IsPrimaryOffice == true))
          .FirstOrDefault();

foreach (var assignee in rootObj.Assignees)
{
        Console.WriteLine("ID = " + assignee.ID);
            Console.WriteLine("IsPrimaryOffice = " + assignee.IsPrimaryOffice);
}

then got the output as

ID = 3333
IsPrimaryOffice = True
ID = 4444
IsPrimaryOffice = False

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

1 Comment

Thank you so much! This worked flawlessly. From your example, I completely understand it now - nested LINQ can get a bit confusing for me.

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.