[英]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. 我试图从具有2个外键的表中检索数据,并使用where子句对其进行过滤。 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.
它是一个Web API场景,当我调用该方法的url端点时,它将返回http代码200,但没有数据,也不会返回任何错误。 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. 请注意,StudentCommentDTO只是一个数据传输对象,我也尝试删除了where子句,但是它仍然没有用,我还尝试了使我的导航属性变为虚拟,但结果仍然相同。 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). 我想我已经知道问题出在哪里了,该反馈表存储了我想要的数据,该数据必须存储在Student和Company的外键中,在每种情况下,它们中的一个必须为null(我可以使其成为可为空)。 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. 我假设您的
studentId
是Student
班的Id
。 Remember if any Id is null
then the full join
record will be missing in returned list (reason: can't join on null
). 请记住,如果任何Id为
null
则返回列表中将缺少完整的join
记录(原因:不能在null
上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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.