LINQ JOIN

    class Department
    {
        //Auto-implemnted propeties
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString()
        {
            return string.Format("{0}", Name);
        }
    }
    class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public int departmentId { get; set; }
        public override string ToString()
        {
            return string.Format("{0} {1}", Name, Title);
        }
    }
    class CorporateData
    {
        public static Department[] departments =
        {
            //Object initializers let you assign values to any accessible fields or properties of an object without having to exlicitly invoke a constructor
             new Department(){ Name = "FMS",Id = 1},
             new Department(){ Name = "MFE",Id =2},
             new Department(){ Name = "BI",Id = 3},
             new Department(){Name = "Detego",Id = 4}
        };
        public static Employee[] employees =
        {
            new Employee () {Id = 1,Name = "PerryDe",Title = "Test Leader",departmentId = 1},
            new Employee () {Id = 2,Name = "Steven Qi",Title = "SDE",departmentId = 1},
            new Employee () {Id = 3,Name = "Guo Hu",Title = "SDE",departmentId = 1},
            new Employee () {Id = 4,Name = "Shan Huo",Title = "SDE",departmentId = 2},
            new Employee () {Id = 5,Name = "Ming Hao",Title = "SDET",departmentId = 3},
            new Employee () {Id = 6,Name = "Xiao Yao",Title = "SDET",departmentId = 3}
        };

    }
    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<Department> departments = CorporateData.departments;
            Console.WriteLine("Department Name:");
            Print(departments);

            Console.WriteLine();
            IEnumerable<Employee> employees = CorporateData.employees;
            Console.WriteLine("Employee name and title:");
            Print(employees);

            Console.WriteLine();
            Console.WriteLine("Query employee informations using LINQ Join");

            var DepartmentEmployeeInfo = from employee in employees
                                         join department in departments on employee.departmentId equals department.Id
                                         select new
                                         {
                                             employeeName = employee.Name,
                                             employeeTitle = employee.Title,
                                             departmentName = department.Name
                                         };
            foreach (var employee in DepartmentEmployeeInfo)
            {
                Console.WriteLine("{0} {1} {2}", employee.employeeName, employee.employeeTitle, employee.departmentName);
            }
                                         
        }
        public static void Print<T>(IEnumerable<T> item)
        {
            foreach (T t in item)
            {
                Console.WriteLine(t);
            }
        }
    }

LINQ Join操作是用于将两个序列中的元素进行匹配,然后将它们合并为一个新的序列。它与SQL语句中的JOIN操作类似。Join操作需要两个输入源,通常是两个集合或表格,以及一个键选择器,它用于指定用于匹配元素的键。Join操作返回一个新的序列,其中包含匹配的元素对。 下面是一个简单的LINQ Join操作示例: ```csharp var customers = new List<Customer> { new Customer {Id = 1, Name = "Alice", City = "New York"}, new Customer {Id = 2, Name = "Bob", City = "Chicago"}, new Customer {Id = 3, Name = "Charlie", City = "Los Angeles"} }; var orders = new List<Order> { new Order {Id = 1, CustomerId = 1, ProductName = "Product A"}, new Order {Id = 2, CustomerId = 2, ProductName = "Product B"}, new Order {Id = 3, CustomerId = 2, ProductName = "Product C"}, new Order {Id = 4, CustomerId = 3, ProductName = "Product D"} }; var customerOrders = from customer in customers join order in orders on customer.Id equals order.CustomerId select new { CustomerName = customer.Name, ProductName = order.ProductName }; foreach (var item in customerOrders) { Console.WriteLine($"Customer Name: {item.CustomerName}, Product Name: {item.ProductName}"); } ``` 在上面的示例中,我们使用Join操作将客户和订单集合中的元素进行匹配,并将匹配的结果放入一个新的匿名对象中。最后,我们通过foreach循环遍历新的序列并输出结果。 希望这个例子能够帮助你理解LINQ Join操作的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值