那么 include call 和 return without cannot implicit convert 如何

How then include call and return without cannot implicit convert

我创建了一个存储库,用于从包含 SchoolIdCourseId.

的中间模式调用值

我的模式结构如下。

学校

 public class Schools
{
    public Guid ID { get; set; }
    public string BannerUrl { get; set; }
    public string Name { get; set; }
    public ICollection<SchoolCourses> SchoolCourses { get; set; }
}

课程

public partial class Course
{
    public Guid ID { get; set; }
    public string Name { get; set; }
    public ICollection<SchoolCourses> SchoolCourses { get; set; }
}

中级 table 即:SchoolCourse

public partial class SchoolCourses
{
    public Guid ID { get; set; }

    public Guid SchoolsID { get; set; }
    public Guid CoursesId { get; set; }

    [ForeignKey("CoursesId")]
    public Course Courses { get; set; } // one school can have multiple courses.
    [ForeignKey("SchoolsID")]
    public Schools Schools { get; set; } // one course can have Multiple School.
}

现在我正在根据上述模式创建一个存储库方法,并希望根据学校 ID 显示学校中所有可用的课程。

这里是存储库class:

public class SchoolCoursesRepository : ISchoolCoursesRepository
{
    public readonly learning_gpsContext _GpsContext = null;
    public SchoolCoursesRepository(learning_gpsContext GpsContext)
    {
        _GpsContext = GpsContext;
    }

    public async Task<ICollection<SchoolCourses>> GetSchoolCourseForSchoolAsync(Guid schoolId)
    {
        var schoolsCourse = await _GpsContext.School.Where(x => x.ID == schoolId)
                           .Include(e => e.SchoolCourses)
                           .ThenInclude(c => c.Courses)
                           .ToListAsync();

        List<SchoolCoursesVm> schoolCoursesVms = new List<SchoolCoursesVm>();
         foreach (var course in schoolsCourse.SelectMany(x => x.SchoolCourses.Select(y => y.Courses)))
         {
             schoolCoursesVms.Add(new SchoolCoursesVm
             {

                  CourseName = course.Name,

             });
         }
         return schoolCoursesVms;
        }

SchoolCoursesVm:

public class SchoolCoursesVm
{
    public string Id {get; set;}
    public string CourseName { get; set; }
}

我的问题是:

  1. 无法找到 return 值的明确类型。
  2. 无法在 get api 控制器中调用 GetSchoolCourseForSchoolAsync 方法。

您 return 键入 ICollection<SchoolCourses> ,但尝试 return List<SchoolCoursesVm>

更改 return 类型以匹配预期类型

public async Task<ICollection<SchoolCoursesVm>> GetSchoolCourseForSchoolAsync(Guid schoolId) {
    var schoolsCourse = await _GpsContext.School.Where(x => x.ID == schoolId)
                       .Include(e => e.SchoolCourses)
                       .ThenInclude(c => c.Courses)
                       .ToListAsync();

    List<SchoolCoursesVm> schoolCoursesVms = new List<SchoolCoursesVm>();
    foreach (var course in schoolsCourse.SelectMany(x => x.SchoolCourses.Select(y => y.Courses))) {
        schoolCoursesVms.Add(new SchoolCoursesVm
        {
            Id = course.Id,
            CourseName = course.Name,
        });
    }
    return schoolCoursesVms;
}