How to add variable from another class to list<T> in entity framework core?

68 Views Asked by At

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. 1-to-n: with the Person entity;
  2. n-to-1: with the Year entity;
  3. n-to-n: with the Group entity;
  4. 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!

1

There are 1 best solutions below

0
On

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.

public async Task<List<(Enrollment, int)>> GetMenteesGroup()
{
    string UserId = "1";
    var ActiveYear = _context.Years.Where(x => x.IsActive).FirstOrDefault();
    List<(Enrollment, int)> myMentees = new List<(Enrollment, int)>();

    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");
                if (mentee != null)
                {
                    myMentees.Add((mentee, group.GroupId));
                }
            }
        }
    }
    else 
    { 
        return myMentees;
    }

    return myMentees;
}