使用带有join的linq查询查询嵌套对象

时间:2016-08-23 17:02:31

标签: c# json.net linq-to-objects

我从api获得了以下json结构:

    {
    "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"
                }
            ]
        }
    ]
}

我使用JSON.NET将其反序列化为以下.net对象:

    [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<StaffMember> StaffMembers { get; set; }

        [JsonPropertyAttribute("visits")]
        public List<Visit> Visits { get; set; }

        [JsonPropertyAttribute("people")]
        public List<Student> 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; }
    }

我使用以下代码反序列化:

var event = (EventInstance)JsonConvert.DeserializeObject(json, typeof(EventInstance));

以上工作正常,并为我提供了上述json结构的精确对象表示。现在我正在尝试查询此事件对象以过滤/项目到一个新结构,然后我可以序列化回json并发送到浏览器。我需要返回一份学生名单,列出处于“完成状态”的活动。和#34;完成&#34;的状态。如您所见,people数组与访问数组绑定(带有id和person_id)。我想生成以下简单输出:

  1. 11112,Brown Belt Karate Class,John Smith,111,Jane Doe
  2. 11112,Brown Belt Karate Class,John Smith,222,Josh Smith
  3. 我尝试过这样的事情:

    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" 
                        && 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);
    

    当然,这会产生重复的学生姓名。我是linq的新手。基本上我需要加入/绑定人员和访问数组,所以我只是为这个id返回一个学生,以及这个事件的根数据。有关更好的方法的任何建议也非常感谢!

1 个答案:

答案 0 :(得分:1)

诀窍是将学生和访问加入到包含以下两者数据的集合中:

from ei in eventInstances
from sm in ei.StaffMembers
from x in
(from vi in ei.Visits
 join st in ei.Students on vi.PersonId equals st.Id
 select new { vi, st }) // Here you get students and visits side-by-side
select new
{
    ei.EventId,
    Event = ei.Name,
    StaffMemeber = sm.Name,
    PersonId = x.st.Id,
    Student = x.st.Name
}