使用Linq使用DTO返回多个值

时间:2016-04-09 06:34:15

标签: entity-framework dto

//This is student model
namespace StudentApp.Models
{
    public class Student
    {
        [Key]
        public int studentId { get; set; }
        public List<AssignedSupervisor> assigned_supervisorId { get; set; }
    }
    public class AssignedSupervisor
    {
        [Key]
        public int supervisorId { get; set; }
    }
}

基于学生模型,实体框架创建了以下数据库

//Here is the sample db structure
//Table Students
studentId
---------
    1
    2
//Table Supervisors
supervisorId | studentId
------------   ---------
     101           1
     102           1
     105           2

studentId是Supervisor表中的外键。学生可以分配一名或多名主管。 所需输出应类似于以下示例:

{
  "studentId": "1",
  "supervisorId": [101,102]
},
{
  "studentId": "2",
  "supervisorId": [105]
}

以下是DTO

//This is student DTO
namespace StudentApp.DTOs
{
    public class StudentDTO
    {
        public int studentId { get; set; }
        public List<AssignedSupervisor> assigned_supervisorId { get; set; }
    }
}

在我的控制器中

namespace ContractingApp.Controllers
{
    //first way I'm doing which returns ugly JSON.
    public List<StudentDTO> GetStudents()
    {
        return db.Students.Select(s => new StudentDTO()
        {
            studentId = s.studentId,
            //Not sure how to use the select statement with it
            assigned_supervisorId = s.Supervisors.Where(
                u => u.studentId == s.studentId
                ).ToList()
        }).ToList();
    }

    //Another way I have tried after the suggestions by @darth_phoenixx I'm trying which doesn't return correct correct format either.
    //It returns 3 JSON objects - one with each supervisor ID.
    public List<dynamic> GetStudents()
    {
        var students = (from t1 in db.Students
                        join t2 in db.Supervisors on t1.studentId equals t2.studentId
                        select new
                        {
                           t1.studentId,
                           t2.supervisorId
                        });
        return students.ToList<dynamic>();
    }
}

任何要解决的文章或方向都会有所帮助。

1 个答案:

答案 0 :(得分:0)

我在stackoverflow上找到了一个提示。感谢@Aducci在Mapping Linq Query results to a DTO class

上回答问题

问题在于我的DTO。正确的DTO应该是:

//SupervisorId is in another DTO and has the correct data type as it is in the DB.
namespace StudentApp.DTOs
{
    public class StudentDTO
    {
        public int studentId { get; set; }
        public IEnumerable<SupervisorDTO> assigned_supervisorId { get; set; }
        //assigned_supervisorId in Student Model needs to be IEnumerable to work with this change in DTO.
    }
    //Separate DTO for supervisor
    public class SupervisorDTO
    {
        public int supervisorId { get; set; }
    }
}

一旦DTO被排序,stackoverflow答案有助于完成查询的“链接”。对于每个查询,都需要使用相关的DTO。这是我的WebAPI中的最终控制器。 (db是实体框架的对象)

public IEnumerable<StudentDTO> GetStudents()
{
    return (from t1 in db.Students
            select new StudentDTO()
            {
                studentId = t1.StudentId,
                assigned_supervisorId = (from t2 in db.Supervisors
                                where t1.StudentId == t2.StudentId
                                select new SupervisorDTO() // Supervisor DTO needs to be used here
                                {
                                    supervisorId = t2.supervisorId
                                })
            });
}