I have two independent tables, "Task" and "Report" with a common column "CustomerId".
I wish to group by these two tables by CustomerId and combine their results into a new list.
If some task or report does not have a matching customerId, then that task/report should have an independent group with their own customerId as key and count 1 as shown below in the required result.
How can I achieve this in LINQ and efcore?
Table1: Task
TaskId | Description | CustomerId |
---|---|---|
1 | Task1 | 1 |
2 | Task2 | 1 |
3 | Task3 | 2 |
4 | Task4 | 7 |
Table2: Report
ReportId | Summary | CustomerId |
---|---|---|
1 | Report1 | 1 |
2 | Report2 | 2 |
3 | Report3 | 3 |
Want to get this result ⬇️
Required Result
Group 1: CustomerId: 1 Count(3)
TaskId 1
TaskId 2
ReportId 1
--------------------------------------
Group 2: CustomerId: 2 Count(2)
TaskId 3
ReportId 2
------------------------------------------
Group 3: CustomerId: 3 Count(1)
ReportId 3
------------------------------------------
Group 4: CustomerId: 7 Count(1)
TaskId 4
I can join two tables and group by them but then only get matched records, I want to get all records, I have not come across a solution as yet and I would truly appreciate any help. --Update--
public class GroupedItems<T> where T : class
{
public string GroupName { set; get; }
public int GroupCount { get; set; }
public IEnumerable<T> Items { set; get; }
}
I wish to get
GroupedItems<ReportTaskDTO>
where if group name is CustomerId 1 then Items should have two task and one report and group count should be 3.