i am begginer with entity framework and radzen blazor, i have a datagrid and i need to put a details from enrollments. Here's a brief explanation of the relationships of the Enrollement entity:
- 1-to-n: with the Person entity;
- n-to-1: with the Year entity;
- n-to-n: with the Group entity;
- n-to-1: with the Course entity.
I have this code in my:
public async Task<List<Enrollment>> GetMenteesGroup()
{
string UserId = "1";
var ActiveYear = _context.Years.Where(x => x.IsActive).FirstOrDefault();
List<Enrollment> myMentees = new List<Enrollment>();
if (ActiveYear != null)
{
var userEnrollments = _context.Enrollments
.Where(x => x.User.Id == UserId && x.YearId == ActiveYear.YearId)
.Include(e => e.Groups)
.ToList();
foreach (var userEnrollment in userEnrollments)
{
var group= _context.Groups.Include(g => g.Enrollments)
.ThenInclude(e => e.Course)
.Include(g=>g.Enrollments)
.ThenInclude(e => e.User)
.FirstOrDefault(x => x.Enrollments.Any(e => e.Id == userEnrollment.Id));
if (group != null)
{
Enrollment mentee = group.Enrollments.FirstOrDefault(e => e.Role == "Mentee");
myMentees.Add(mentee != null ? mentee : null);
}
}
}
else
{
return myMentees;
}
return myMentees;
}
Where i have if (group != null)
, i need to save too group.groupId
, and the last Meeting
date inside group.Meentings
.
Sorry I'm a newbie, if you can help me I'd be grateful!
I solved my problem by adding a variable in the list to be returned, if I want to return more variables just add more items.