解决了吗?如何使用相关DTO的嵌套列表返回DTO列表

时间:2019-07-19 01:56:12

标签: c# asp.net-core linq-to-sql entity-framework-core dto

在不使用Automapper的情况下,我试图将DTO的列表映射到DTO的列表的每个元素中。具体地说,在此示例中,我希望API返回一个父母列表,其中每个父母都包含它的孩子列表和宠物列表。

我有一个带有以下模型的EF Core项目(该模型使用完全定义的关系),并且存在于较大的数据库中。

有一个Parent对象,其中包含许多Pet对象和许多Child对象,例如:

EF核心模型

    public class Parent
    {
        public int ParentId { get; set; }
        public string Name { get; set; }

        public List<Child> Children { get; set; }
        public List<Pet> Pets { get; set; }
    }

    public class Child
    {
        public int ChildId { get; set; }
        public string Name { get; set; }

        public int ParentId { get; set; }
        public Parent Parent { get; set; }
    }

    public class Pet
    {
        public int PetId { get; set; }
        public string Name { get; set; }

        public int ParentId { get; set; }
        public Parent Parent { get; set; }
    }

我不使用Automapper,而是尝试在控制器中使用DTO将数据发送出去。

我已经按照以下步骤设置了DTO:

DTO

    public class ParentDTO
    {
        public int ParentId { get; set; }
        public string Name { get; set; }

        //Should these be List<> or IEnumerable<>?
        public IEnumerable<ChildDTO> Children { get; set; }
        public IEnumerable<PetDTO> Pets { get; set; }
    }

    public class ChildDTO
    {
        public int ChildId { get; set; }
        public string Name { get; set; }
    }

    public class PetDTO
    {
        public int PetId { get; set; }
        public string Name { get; set; }
    }

当前我的控制器如下:

控制器尝试1

        // GET: api/Parents/
        [HttpGet]
        public IEnumerable<ParentDTO> GetParents()
        {

            var parents = from pa in _context.Parents
                          select new ParentDTO()
                          {
                              Name = pa.Name
                          };

            return parents;
        }

        // GET: api/Parents/GetParentsWith
        [HttpGet("[action]")]
        public IEnumerable<ParentDTO> GetParentsWith()
        {

            var parents = from pa in _context.Parents
                          select new ParentDTO()
                          {
                              Name = pa.Name,
                              //Not sure how to correctly do this below...
                              Children = (from c in _context.Children.Where(c => c.ParentId == pa.ParentId)
                                          select new ChildDTO()
                                          {
                                              Name = c.Name
                                          }),
                              Pets = (from pe in _context.Pets.Where(pet => pet.ParentId == pa.ParentId)
                                      select new PetDTO()
                                      {
                                          Name = pe.Name
                                      })
                          };

            return parents;
        }

我的GET: api/Parents/正常工作(因为我不想创建嵌套列表),但是我如何像在{中尝试的那样,将相关的子项和宠物项添加到父DTO中{1}}?

尝试此操作会出现错误:

GET:  api/Parents/GetParentsWith

我也尝试过:

控制器尝试2

Microsoft.Data.SqlClient.SqlException (0x80131904): Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
....
...

这也会产生错误:

        // GET: api/Parents/GetParentsWith
        [HttpGet("[action]")]
        public IEnumerable<ParentDTO> GetParentsWith()
        {

            var parents = from pa in _context.Parents
                          select new ParentDTO()
                          {
                              Name = pa.Name,
                              Children = (from c in pa.Children
                                          select new ChildDTO()
                                          {
                                              Name = c.Name
                                          }),
                              Pets = (from pe in pa.Pets
                                      select new PetDTO()
                                      {
                                          Name = pe.Name
                                      })
                          };

            return parents;
        }

我也尝试过:

控制器尝试3

Microsoft.Data.SqlClient.SqlException (0x80131904): Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
....
...

不会引发任何错误所以也许我在正确的轨道上,不幸的是,它只是返回空值,而我希望其中有一系列的孩子或宠物:

        // GET: api/Parents/GetParentsWith
        [HttpGet("[action]")]
        public IEnumerable<ParentDTO> GetParentsWith()
        {

            var parents = from pa in _context.Parents
                          select new ParentDTO()
                          {
                              ParentId = pa.ParentId,
                              Name = pa.Name
                          };

            foreach (ParentDTO parent in parents)
            {
                var children = from child in _context.Children where child.ParentId == parent.ParentId
                               select new ChildDTO()
                               {
                                   ChildId = child.ChildId,
                                   Name = child.Name
                               };

                var pets = from pet in _context.Pets
                           where pet.ParentId == parent.ParentId
                           select new PetDTO()
                           {
                               PetId = pet.PetId,
                               Name = pet.Name
                           };

                parent.Children = children;
                parent.Pets = pets;

            return parents;
        }

已解决!但是我做对了吗?

我注意到[ { "parentId": 1, "name": "Ronald", "children": null, "pets": null }, { "parentId": 2, "name": "Benjamin", "children": null, "pets": null }, { "parentId": 3, "name": "Sally", "children": null, "pets": null }, { "parentId": 4, "name": "Foobar", "children": null, "pets": null } ] 是一个IQueryable,因此foreach中的分配没有按我预期的那样工作...我想我需要学习一些有关IQueryable,IEnumerable等的信息。

parents

0 个答案:

没有答案