简体   繁体   中英

retrieving data from a table with 2 foreign keys in c# asp.net web api

I am trying to retrieve data from a table which has 2 foreign keys and filter it with a where clause. Its a web api scenario and when I call the url endpoint of that method it returns http code 200 but with no data, no error are returned as well. Below is the model which is the basis for my table

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; }

    }
}

The code in the controller which has the method which that I am using to retrieve data is below

    [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;
    }

Note that StudentCommentDTO is just a data transfer object, I have also tried to remove the where clause but it still didn't work, I have also tried to make my navigatation properties virtual but the result is still the same. I can boldly confirm that there is data in the table. The screen shot below shows the result I am getting in the fiddler, an empty array is returned always click here to see results from the fiddler

I think I have seen where my problem is, The Feedbacks table which stores the data that I want has to foreign keys for Student and Company in each case one of them has to be null (I have made it possible to be nullable). I am trying to archieve a situation where by people with 2 different roles, companies and students can participate in comments of a particular post

I have assumed your studentId is Id in Student Class. Remember if any Id is null then the full join record will be missing in returned list (reason: can't join on 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM