包含对象列表的 IEnumerable 在哪里
Where to IEnumerable that contains a list of objects
我是 EntityFrameworkCore 和 Asp.Net Core 的新手,我遇到了以下问题。
我有 2 个表,文凭和教师,我正在尝试 select 具有给定的 TeacherName 的文凭。我使用 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
这个查询比您尝试创建的要简单得多。如果您已正确定义导航属性,通常不需要创建 JOIN。
public IEnumerable<Diploma> GetAllDiplomasForTeachers(string username)
{
var query = context.Diplomas
.Include(d => d.Teachers)
.Where(d => d.Teachers.Any(t => t.Name == userName));
return query;
}
我是 EntityFrameworkCore 和 Asp.Net Core 的新手,我遇到了以下问题。 我有 2 个表,文凭和教师,我正在尝试 select 具有给定的 TeacherName 的文凭。我使用 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
这个查询比您尝试创建的要简单得多。如果您已正确定义导航属性,通常不需要创建 JOIN。
public IEnumerable<Diploma> GetAllDiplomasForTeachers(string username)
{
var query = context.Diplomas
.Include(d => d.Teachers)
.Where(d => d.Teachers.Any(t => t.Name == userName));
return query;
}