【问题标题】:Linq Entity Framework: select table data without repeated data into IEnumerableLinq Entity Framework:选择没有重复数据的表数据放入IEnumerable
【发布时间】:2017-11-10 02:32:20
【问题描述】:

我正在使用以下技术:C#、SQL Server、ASP.NET 和 Entity Framework 以及 Linq。

我的数据库中有一个多对多关系:

模型类:

public class Courses
{
    [Required]
    public int Id { get; set; }
    //more properties here
     public student stud { get; set; }
}

public class inscribe
{
    [Key]
    public intId { get; set; }
    //properties here
    public student student{ get; set; }
    [Required]
    [ForeignKey("student")]
    public string StudentId{ get; set; }
    public Courses Courses{ get; set; }
}

给定一个学生 ID,我想返回一个他/她被登记的课程列表。

这是我迄今为止尝试过的:

public IEnumerable<CursoDto> GetCursosAlumno(Int studentId)
{
     //some code here to validate
     var x = _dbContext
                 .Inscribe.Include(c => c.Courses)
                 .Where(c => c.StudentId == studentId).toList();
     // x variable is a list<inscribe>
}

我的问题是我不知道如何访问课程实体并将其作为列表返回,例如:

var result = X.Courses;
return result; //result is a list<courses>

我该怎么做?如果可能,请不要使用 foreach 块。

谢谢

【问题讨论】:

    标签: c# asp.net entity-framework linq


    【解决方案1】:

    在 Code First 方法中,您不需要将“链接表”(OP 中的inscribe)添加到您的模型中(它将被透明地创建)。

    //Models
    public class Course
    {
        [Key]
        public int Id { get; set; }
        //more properties here
         public virtual /*important*/ ICollection<Student> studs { get; set; }
     }
    public class Student
    {
        [Key]
        public int Id { get; set; }
        //more properties here
         public virtual /*important*/ ICollection<Course> courses { get; set; }
     }
    
     //Controller
    var stud = _dbContext.studs.Where(s => s.Id == /*param*/id).FirstOrDefault();
    var courses = stud.courses.ToList();//Real courses with id, name, etc. No Include required
    

    更新
    如果您确实需要“链接表”(例如添加一些属性,如sortOrderenrollmentDate),那么模型会有所不同。

    //Models
    public class Course
    {
        [Key]
        public int Id { get; set; }
        //more properties here
         public virtual /*important*/ ICollection<StudentCourse> studs { get; set; }
     }
    public class Student
    {
        [Key]
        public int Id { get; set; }
        //more properties here
         public virtual /*important*/ ICollection<StudentCourse> courses { get; set; }
     }
    [Table("inscribe")]
    public class StudentCourse
    {
        [Key, Column(Order = 0)]
        public int StudentId {get; set'}
        [Key, Column(Order = 1)]
        public int CourseId {get; set'}
        //extra properties
        [ForeignKey("StudentId")]
        public virtual Student stud { get; set; }
        [ForeignKey("CourseId")]
        public virtual Course course { get; set; }
    }
    
    //Controller
    var courses = _dbContext.courses.Where(c/*c is "link"*/ => c.Student/*StudentCourse prop*/.Any(s/*link again*/ => s.StudentId == someId/*param*/));//again courses
    

    如您所见,Include 不是必需的。

    【讨论】:

    • 您好,感谢您的建议。我注意到您在控制器内建议的代码错过了题字表。你能更新吗?谢谢
    • 感谢您的更新,我有一个问题,当我使用虚拟属性时,我启用了延迟加载,但是我可以通过急切加载来实现您的建议吗?
    【解决方案2】:
    var result = _dbContext
        .Inscribe.Include(c => c.Courses)
        .Where(c => c.StudentId == studentId)
        .SelectMany(c => c.Courses).ToList();
    

    【讨论】:

    • 你好,我试过了,但我收到一个错误:方法的类型元素....无法从使用中推断出来。尝试明确指定参数。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多