在哪里包含对象列表
我是EntityFrameWorkCore和ASP.NET Core的新手,我有以下问题。 我有2张桌子,文凭和老师,我正在尝试选择具有授课名称的文凭。我使用join()来内部加入2个表,但我不断收到错误。我该如何实现这一目标,还是还有其他方法可以做到这一点? 使用ASP.NET Core 5.0和EntityFrameWorkCore 5.0
模型
public class Diploma
{
public Diploma(){
this.Status = "Pending";
}
public int Id { get; set; }
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Teachers")]
public List<Teacher> Teachers { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
[Display(Name = "Student Name")]
public string StudentName { get; set; }
public string Status { get; set; }
[Display(Name = "FilePath")]
public string FilePath { get; set; }
}
public class Teacher
{
public int TeacherId { get; set; }
[Display(Name = "TeacherName")]
public string Name { get; set; }
public int DiplomaId { get; set; }
public Diploma Diploma { get; set; }
}
sqlrepository
public IEnumerable<Diploma> GetAllDiplomasForTeachers(string username)
{
Teacher t = new Teacher();
t.Name = username;
var query = context.Diplomas.Join(
context.Teachers,
diploma => diploma.Id,
teacher => teacher.DiplomaId,
(diploma, teacher) => new Diploma
{
Id = diploma.Id,
Title = diploma.Title,
Description = diploma.Description,
StudentName = diploma.StudentName,
Teachers = context.Teachers.ToList<Teacher>(),
Status = diploma.Status,
FilePath = diploma.FilePath
}
).Where(a => a.Teachers.Contains(t)); //This line produces[enter image description here][1] the following error
return query;
}
控制器
public ViewResult ShowDiplomas()
{
var userName = User.FindFirstValue(ClaimTypes.Name);
var diploma = _diplomaRepository.GetAllDiplomasForTeachers(userName);
if (diploma == null)
{
ViewBag.ErrorMessage = $"No diploma is created yet";
}
return View(diploma);
}
数据库表
Teachers
TeacherId,Name,DiplomaId
Diplomas
Id,Title,Description,StudentName,Status,FilePath
I new to EntityFrameworkCore and Asp.Net Core and I have the following problem.
I have 2 tables, Diplomas and Teachers and I am trying to select the diplomas that have as TeacherName the given one. I use Join() in order to inner join the 2 tables but I keep receiving an error. How can I achieve this, or is there some other way to do it ?
Using Asp.Net Core 5.0 and EntityFrameworkCore 5.0
Models
public class Diploma
{
public Diploma(){
this.Status = "Pending";
}
public int Id { get; set; }
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Teachers")]
public List<Teacher> Teachers { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
[Display(Name = "Student Name")]
public string StudentName { get; set; }
public string Status { get; set; }
[Display(Name = "FilePath")]
public string FilePath { get; set; }
}
public class Teacher
{
public int TeacherId { get; set; }
[Display(Name = "TeacherName")]
public string Name { get; set; }
public int DiplomaId { get; set; }
public Diploma Diploma { get; set; }
}
SQlRepository
public IEnumerable<Diploma> GetAllDiplomasForTeachers(string username)
{
Teacher t = new Teacher();
t.Name = username;
var query = context.Diplomas.Join(
context.Teachers,
diploma => diploma.Id,
teacher => teacher.DiplomaId,
(diploma, teacher) => new Diploma
{
Id = diploma.Id,
Title = diploma.Title,
Description = diploma.Description,
StudentName = diploma.StudentName,
Teachers = context.Teachers.ToList<Teacher>(),
Status = diploma.Status,
FilePath = diploma.FilePath
}
).Where(a => a.Teachers.Contains(t)); //This line produces[enter image description here][1] the following error
return query;
}
Controller
public ViewResult ShowDiplomas()
{
var userName = User.FindFirstValue(ClaimTypes.Name);
var diploma = _diplomaRepository.GetAllDiplomasForTeachers(userName);
if (diploma == null)
{
ViewBag.ErrorMessage = quot;No diploma is created yet";
}
return View(diploma);
}
Database Tables
Teachers
TeacherId,Name,DiplomaId
Diplomas
Id,Title,Description,StudentName,Status,FilePath
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试创建的查询要简单得多。通常,如果您具有正确定义的导航属性,则不需要创建加入。
This query is much simpler that you have tried to create. Usually it is not needed to create JOINs if you have properly defined navigation properties.