在c#asp.net web api中从具有2个外键的表中检索数据

时间:2015-12-03 17:02:20

标签: c# asp.net entity-framework linq

我正在尝试从具有2个外键的表中检索数据,并使用where子句对其进行过滤。它是一个web api场景,当我调用该方法的url端点时,它返回http代码200但没有数据,也没有返回错误。以下是我的表

的基础模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace xxxx.Models
{
    public class Feedback
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string comment { get; set; }
        public string date { get; set; }

        //Foreign key
        [Required]
        public int projectId { get; set; }
        public int studentId { get; set; }
        public int companyId { get; set; }

        [ForeignKey("studentId")]
        public  Student student { get; set; }

        [ForeignKey("companyId")]
        public Company company { get; set; }

    }
}

控制器中的代码具有我用来检索数据的方法

    [Route("GetComments")]
    [HttpGet]
    public IQueryable<CommentDTO> GetFeedbacks(int project_id)
    {
        var comments = from b in db.Feedbacks.Where(b => b.projectId == project_id)
                       .Include(b=> b.company)
                       .Include(b=> b.student)

                       select new CommentDTO
                       {
                           Id = b.Id,
                           comment = b.comment,
                           date = b.date,
                           student = new StudentCommentDTO()
                           {
                               student_number = b.student.Id,
                               first_name = b.student.firstName,
                               middle_name = b.student.middleName,
                               last_name = b.student.lastName,
                               profile_pic = b.student.profilePic
                           },
                           company = new CompanyCommentDTO()
                           {
                               companyID = b.company.Id,
                               profile_pic = b.company.profilePicture,
                               name = b.company.companyName
                           }
                       };
        return comments;
    }

请注意,StudentCommentDTO只是一个数据传输对象,我也尝试删除where子句但它仍然没有工作,我也尝试将我的导航属性设置为虚拟但结果仍然相同。我可以大胆地确认表中有数据。下面的屏幕截图显示了我在fiddler中获得的结果,总是返回一个空数组 click here to see results from the fiddler

我想我已经看到了我的问题所在,反馈表将我想要的数据存储到学生公司的外键中它们必须为null(我已经可以为空)。我试图通过具有两种不同角色的人,公司和学生可以参与特定职位的评论来实现这种情况

1 个答案:

答案 0 :(得分:0)

我假设您的studentIdId类中是Student。请记住,如果任何ID是null,则完整的join记录将在返回列表中丢失(原因:无法加入null)。

        IQueryable<CommentDTO> comments = null;
        comments = from b in db.Feedbacks.Where(b => b.projectId == project_id)
                       join std in db.Students on b.studentId equals std.Id
                       join cmp in db.Companies on b.companyId equals cmp.Id 

                       select new CommentDTO
                       {
                           Id = b.Id,
                           comment = b.comment,
                           date = b.date,
                           student = new StudentCommentDTO()
                           {
                               student_number = std.Id,
                               first_name = std.firstName,
                               middle_name = std.middleName,
                               last_name = std.lastName,
                               profile_pic = std.profilePic
                           },
                           company = new CompanyCommentDTO()
                           {
                               companyID = cmp.Id,
                               profile_pic = cmp.profilePicture,
                               name = cmp.companyName
                           }
                       };
        return comments.ToList();