在哪里包含对象列表

发布于 2025-01-31 08:17:45 字数 2706 浏览 3 评论 0原文

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2025-02-07 08:17:45

您尝试创建的查询要简单得多。通常,如果您具有正确定义的导航属性,则不需要创建加入。

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

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文