Я создаю метод Linq и хочу присоединиться к таблице, чтобы получить среднюю оценку на основе «SchoolSystemId», переданного в аргументах.
public async Task<ICollection<GetGradeLevelAverageVm>> GetSchoolSystemGradLevelsAverage(Guid schoolSystemId)
{
var tem = _GpsContext.AssignmentStudents
.Include(a => a.Assignment) // Include all Childs..
.ThenInclude(l=> l.Lessons)
.ThenInclude(u => u.Units)
.ThenInclude(c => c.Course)
.ThenInclude(g => g.GradeLevels)
.ThenInclude(s=>s.SchoolSystems)
.Where(s =>s.SchoolSystemsID == schoolSystemId)
.GroupBy(g =>
new
{
GradeLevel = g.Assignment.Lessons.Units.Course.GradeLevels.ID,
})
.Select(g =>
new GetGradeLevelAverageVm
{
GradeLevels = g.Key.GradeLevel.ToString(),
AverageObtainedMarks = g.Average(a => a.ObtainedMarks)
}).ToList();
return tem;
}
Вот код метода get для получения данных в API
[HttpGet()]
public async Task<IActionResult> GetCEOGradeLevelAverage(string schoolSystemId)
{
var overallgradeAverages = await _ceoDashboadRepository.GetSchoolSystemGradLevelsAverage(Guid.Parse(schoolSystemId));
return Ok(overallgradeAverages);
}
И структура ModelView:
public class GetGradeLevelAverageVm
{
public double AverageObtainedMarks { get; set; }
public string GradeLevels { get; set; }
}
А ниже приведен код структуры контекста БД, которую я использовал:
public partial class AssignmentStudents
{
public Guid Id { get; set; }
public int ObtainedMarks { get; set; }
public Guid AssignmentId { get; set; }
[ForeignKey("AssignmentId")]
public Assignment Assignment { get; set; }
}
public partial class Assignment
{
public Guid Id { get; set; }
public Guid LessonId { get; set; }
[ForeignKey("LessonId")]
public Lesson Lessons { get; set; } // lesson wise assignment
public ICollection<AssignmentStudents> assignmentStudents { get; } = new List<AssignmentStudents>();
}
public partial class Course
{
public Guid ID { get; set; }
public Guid GradeLevelsID { get; set; }
[ForeignKey("GradeLevelsID")]
public virtual GradeLevels GradeLevels { get; set; }
public Guid SchoolSystemsID { get; set; }
[ForeignKey("SchoolSystemsID")]
public virtual SchoolSystem SchoolSystems { get; set; }
public virtual ICollection<Units> Units { get; set; }
}
public partial class Units
{
public Guid Id { get; set; }
public Guid CourseID { get; set; }
[ForeignKey("CourseID")]
public virtual Course Course { get; set; }
public virtual ICollection<Lesson> Lessons { get; set; }// in a unit can have multiple lession
}
public partial class Lesson
{
public Guid Id { get; set; }
public Guid UnitsId { get; set; }
[ForeignKey("UnitsId")]
public virtual Units Units { get; set; }
public virtual List<Assignment> Assignment { get; set; }
}
public partial class GradeLevels
{
public Guid ID { get; set; }
public Guid SchoolSystemId { get; set; }
[ForeignKey("SchoolSystemId")]
public SchoolSystems SchoolSystems { get; set; }
}
public abstract class SchoolSystems
{
public Guid Id { get; set; }
public ICollection<GradeLevels> gradeLevels { get; } = new List<GradeLevels>();}
Здесь моя проблема:
- Я не могу получить значение по идентификатору Schoolsystem.
- Я не могу получить значение в модели представления.
- и не могу позвонить, чтобы получить значение в API Json