Я хочу объединить 3 таблицы, которые я получаю в виде списка объектов.Вот мои три таблицы:
Сотрудник
![enter image description here](https://i.stack.imgur.com/lUjcK.png)
Отдел
![enter image description here](https://i.stack.imgur.com/s7i32.png)
Категория
![enter image description here](https://i.stack.imgur.com/7ADST.png)
Сотрудник DepartmentID и CategoryIDиспользуется для присоединения к Отделу и Таблице категорий.
Так выглядит мой Linq Join
var result = from e in Employee.GetAllEmployees()
join d in Department.GetAllDepartments() on e.DepartmentID equals d.ID
join c in Cateory.GetAllCategories() on e.CategoryID equals c.ID
into eGroup
from c in eGroup.DefaultIfEmpty()
select new
{
Employee =e,
Department = d ==null? new Department() : d,
Cateory = c
};
Моя проблема в том, что я получаю две разные строки для идентификатора сотрудника = 1, и это из-за двух разных категорий для идентификатора = 1
![enter image description here](https://i.stack.imgur.com/R8Aev.png)
Я хотел бы получить обе категории в одном и том же узле Employee.В основном две категории для идентификатора сотрудника = 1.
Ожидаемый результат: категории A и категория B привязаны к отметке сотрудника.
![enter image description here](https://i.stack.imgur.com/Bo3Gu.png)
![enter image description here](https://i.stack.imgur.com/gwAKR.png)
Как мне этого добиться?
Спасибо за помощь!
Вот код для воспроизведения того, что у меня есть.
class Program
{
static void Main(string[] args)
{
var result = from e in Employee.GetAllEmployees()
join d in Department.GetAllDepartments() on e.DepartmentID equals d.ID
join c in Cateory.GetAllCategories() on e.CategoryID equals c.ID
into eGroup
from c in eGroup.DefaultIfEmpty()
select new
{
Employee =e,
Department = d ==null? new Department() : d,
Cateory = c
};
Console.WriteLine("Hello World!");
Console.ReadLine();
}
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
public int CategoryID { get; set; }
public static List<Employee> GetAllEmployees()
{
return new List<Employee>()
{
new Employee { EmployeeID = 1, Name = "Mark", DepartmentID = 1, CategoryID = 1 },
};
}
}
public class Department
{
public int ID { get; set; }
public string DepartmentName { get; set; }
public static List<Department> GetAllDepartments()
{
return new List<Department>()
{
new Department { ID = 1, DepartmentName = "TECH"},
new Department { ID = 2, DepartmentName = "HR"},
};
}
}
public class Cateory
{
public int ID { get; set; }
public string CategoryName { get; set; }
public static List<Cateory> GetAllCategories()
{
return new List<Cateory>()
{
new Cateory { ID = 1, CategoryName = "CategoryA"},
new Cateory { ID = 1, CategoryName = "CategoryB"},
new Cateory { ID = 2, CategoryName = "CategoryC"},
};
}
}
}