簡體   English   中英

C#LINQ左聯接3個表/列表

[英]C# LINQ left join 3 Tables/Lists

我希望將3個表作為對象列表連接起來。 這是我的三個表:

雇員

在此處輸入圖片說明

在此處輸入圖片說明

類別

在此處輸入圖片說明

員工DepartmentID和CategoryID用於聯接Department和Category Table。

這就是我的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
                         };

我的問題是,我得到了兩行不同的員工ID = 1,這是因為ID = 1的兩個不同類別

在此處輸入圖片說明

我想將兩個類別都放在同一個Employee節點中。 員工ID = 1基本上是兩個類別。

預期結果:CategoryA和CategoryB與員工標記相關。

在此處輸入圖片說明

在此處輸入圖片說明

我該如何實現?

謝謝您的幫助 !

這是重現我目前所擁有代碼的代碼。

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"},
            };
        }
    }
}

我沒有真正測試此方法的方法,但是您應該能夠通過更新查詢來進行左聯接

 var result = from e in Employee.GetAllEmployees()
                 join d in Department.GetAllDepartments() on e.DepartmentID equals d.ID into d_def
                 from d in d_def.DefaultIfEmpty()
                 join c in Cateory.GetAllCategories() on e.CategoryID equals c.ID into c_def
                 from c in c_def.DefaultIfEmpty())
                 select new
                 {
                     Employee =e,
                     Department = d ==null? new Department() : d,
                     Cateory = c
                 };

這是您的結果。 兩名雇主具有相同的身份證號

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM