C
C#16mo ago
SWEETPONY

✅ How to write special linq group by? How to shorten my code?

I have function which named as GetDepartmentsWithNested. It returns Task<Dictionary<string, string>, IEnumerable<string>> where first Item is children with path (doesn't matter) and second Item is some departments without any children. Then I get subjects for Item1 and Item2 and fill dataSet with them. My problem: it looks ugly, 2 calls of function GetByDepartmentsIds instead of just one Let's see my code:
// returns Dictionary<string, string> and IEnumerable<string> as I said before
var departments = await GetDepartmentsWithNested();

// get subjects and than simple grouping
var groupedSubjects = (await _subjectClient
.GetByDepartmentsIds(departments.Item1.Keys)
.Unwrap()
.GroupBy(subject => (
DepartmentId: subject.Department.Id,
Title: departments.Item1[subject.Department.Id]));

// fill dataset with data
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.Title);
}
// returns Dictionary<string, string> and IEnumerable<string> as I said before
var departments = await GetDepartmentsWithNested();

// get subjects and than simple grouping
var groupedSubjects = (await _subjectClient
.GetByDepartmentsIds(departments.Item1.Keys)
.Unwrap()
.GroupBy(subject => (
DepartmentId: subject.Department.Id,
Title: departments.Item1[subject.Department.Id]));

// fill dataset with data
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.Title);
}
Okey, now we need to get subjects for Item2 with another grouping!
var anotherGroupedSubjects = (await _subjectClient
.GetByDepartmentsIds(departments.Item2)
.Unwrap()
.GroupBy(subject => subject.Department.Title);

foreach(var group in anotherGroupedSubjects)
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);
}
var anotherGroupedSubjects = (await _subjectClient
.GetByDepartmentsIds(departments.Item2)
.Unwrap()
.GroupBy(subject => subject.Department.Title);

foreach(var group in anotherGroupedSubjects)
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);
}
May someone help me with grouping these things, please?
0 Replies
No replies yetBe the first to reply to this messageJoin