C#C
C#3y ago
SWEETPONY

❔ How to extend property for group by?

As an input, I accept a list of departments IDs, for which I get a list of departments and their subjects. The code looks quite simple:

private async Task<IEnumerable<string>> GetDepartmentsIdsWithNested()
{
    var parentsDepartments = await _departmentClient
         .GetByParentsIds( _parameters.DepartmentIDs )
         .Unwrap();

    var ids = parentsDepartments
         .SelectMany( x => new[] { x.Id, x.ParentDepartment.Id } )
         .Distinct();

    return ids;
}

private async Task<StaffDataSet> PrepareDataSet()
{
    var dataSet = new StaffDataSet();

    var departmentsIds = _parameters.ShowSubjectsOfNestedDepartments
         ? await GetDepartmentsIdsWithNested()
         : _parameters.DepartmentIDs;

    var groupedSubjects = ( await _subjectClient
           .GetByDepartmentsIds( departmentsIds )
           .Unwrap() )
         .GroupBy( subject => subject.Department.Title );

    foreach ( var group in groupedSubjects )
        foreach ( var subject in group )
        {
            dataSet.Staff.AddStaffRow(
                Title: subject.GetSubjectFullName(),
                AccessGroup: GetAccessGroupFormattedString( subject.AccessGroups ),
                TabNumber: subject.Data.FromJson<SubjectData>()?.PersonnelNumber,
                Identifier: GetIdentifierFormattedString( subject.Identifiers ),
                Department: group.Key );
        }

    return dataSet;
}


GroupedSubjects are entities that have been grouped by department name, but I would like to group another way. What I mean:
Let's say I have a department "Home", and he has two more "Class 1", "Class 2". Subjects are grouped by Home, Class1, Class2, but I want smth like this: Home, Home/Class 1, Home/Class 2
Was this page helpful?