Querying nested objects using linq query with join
<p>I have the following json structure that I am getting from an api: </p>

<pre><code>    {
    "event_instance": [
        {
            "id": 55551244,
            "event_id": 11112,
            "name": "Brown Belt Karate Class",
            "staff_members": [
                {
                    "id": 12345,
                    "name": "John Smith"
                }

            ],

            "people": [
                {
                    "id": 111,
                    "name": "Jane Doe"
                },
                {
                    "id": 222,
                    "name": "Josh Smith"

                },
                {
                    "id": 333,
                    "name": "Ben Johnson"
                }

            ],
            "visits": [
                {
                    "id": 1234578,
                    "person_id": 111,
                    "state": "completed",
                    "status": "complete"
                },
                {
                    "id": 1239865,
                    "person_id": 222,
                    "state": "completed",
                    "status": "complete"
                },
                {
                    "id": 1239865,
                    "person_id": 333,
                    "state": "canceled",
                    "status": "cancel"
                }
            ]
        }
    ]
}
</code></pre>

<p>I am deserializing this into the following .net objects using JSON.NET:</p>

<pre><code>    [JsonObjectAttribute("event_instance")]
    public class EventInstance
    {
        [JsonPropertyAttribute("id")]
        public int Id { get; set; }

        [JsonPropertyAttribute("event_id")]
        public int EventId { get; set; }

        [JsonPropertyAttribute("name")]
        public string Name { get; set; }

        [JsonPropertyAttribute("staff_members")]
        public List&lt;StaffMember&gt; StaffMembers { get; set; }

        [JsonPropertyAttribute("visits")]
        public List&lt;Visit&gt; Visits { get; set; }

        [JsonPropertyAttribute("people")]
        public List&lt;Student&gt; Students { get; set; }
    } 

    [JsonObjectAttribute("staff_members")]
    public class StaffMember
    {
        [JsonPropertyAttribute("id")]
        public int Id { get; set; }
        [JsonPropertyAttribute("name")]
        public string Name { get; set; }
    }

    [JsonObjectAttribute("people")]
    public class People
    {
        [JsonPropertyAttribute("id")]
        public int Id { get; set; }
        [JsonPropertyAttribute("name")]
        public string Name { get; set; }
    }

    [JsonObjectAttribute("visits")]
    public class Visits
    {
        [JsonPropertyAttribute("id")]
        public int Id { get; set; }
        [JsonPropertyAttribute("person_id")]
        public int PersonId { get; set; }
        [JsonPropertyAttribute("state")]
        public string State { get; set; }
        [JsonPropertyAttribute("status")]
        public string Status { get; set; }
    }
</code></pre>

<p>I am using the following code to de-serialize: </p>

<pre><code>var event = (EventInstance)JsonConvert.DeserializeObject(json, typeof(EventInstance));
</code></pre>

<p>The above works fine and gives me an accurate object representation of the above json structure. Now I am trying to query this event object to filter/project to a new structure that I can then serialize back to json and send down to the browser. I need to return a list of students for the event that are in a state of "completed" and a status of "complete". As you can see, the people array ties to the visits array (with id and person_id). I would like to produce the following simple output:</p>

<ol>
<li>11112, Brown Belt Karate Class, John Smith, 111, Jane Doe</li>
<li>11112, Brown Belt Karate Class, John Smith, 222, Josh Smith</li>
</ol>

<p>I have tried something like this: </p>

<pre><code>var studentList =   from theClass in event
                    from staff in theClass.StaffMembers
                    from student in theClass.People
                    from visits in theClass.Visits
                    where visits.Status == "complete" 
                    &amp;&amp; visits.State == "completed"
                           select new
                           {
                               event_id = theClass.EventId
                               class_name = theClass.Name,
                               instructor_name = staff.Name,
                               student_id = student.Id,
                               student_name = student.Name
                           };



string _data = JsonConvert.SerializeObject(studentList);
</code></pre>

<p>Naturally this produces duplicate student names. I am new to linq. Basically I need to join/tie the people and visits array so I just get back a single student for that id, along with the root data for this event. Any advice on a better way to do this is much appreciated too!</p>