zoukankan      html  css  js  c++  java
  • Linq的常见查询

    首先定义几个模型类:  

       /// <summary>
        /// 员工类
        /// </summary>
        public class Employee
        {
            /// <summary>
            /// 员工id
            /// </summary>
            public int Empid { get; set; }
            /// <summary>
            /// 部门id
            /// </summary>
            public int DeptId { get; set; }
            /// <summary>
            /// 员工姓名
            /// </summary>
            public string EmpName { get; set; }
            /// <summary>
            /// 员工编号
            /// </summary>
            public string EmpCode { get; set; }
        }
        /// <summary>
        /// 部门模型
        /// </summary>
        public class DeptModel
        {
            /// <summary>
            /// 部门id
            /// </summary>
            public int DeptId { get; set; }
            /// <summary>
            /// 部门名称
            /// </summary>
            public string DeptName { get; set; }
        }
    View Code

    组装数据:

         /// <summary>
            /// 员工数据
            /// </summary>
            private static List<Employee> Emp = new List<Employee>()
            {
                new Employee {Empid=1,DeptId=6,EmpName="王芳",EmpCode="1001" },
                new Employee {Empid=2,DeptId=5,EmpName="韩丽",EmpCode="1002" },
                new Employee {Empid=3,DeptId=3,EmpName="李飞",EmpCode="1003" },
                new Employee {Empid=4,DeptId=2,EmpName="李丽",EmpCode="1004" },
                new Employee {Empid=5,DeptId=4,EmpName="王二麻",EmpCode="1005" },
                new Employee {Empid=6,DeptId=1,EmpName="刘慧",EmpCode="1006" },
                new Employee {Empid=7,DeptId=1,EmpName="张飞",EmpCode="1007" },
                new Employee {Empid=8,DeptId=8,EmpName="测试人员",EmpCode="1008" },
            };
            /// <summary>
            /// 部门数据
            /// </summary>
            private static List<DeptModel> Dept = new List<DeptModel>()
            {
                new DeptModel {DeptId=1,DeptName="产品部" },
                new DeptModel {DeptId=2,DeptName="管理层" },
                new DeptModel {DeptId=3,DeptName="人事部" },
                new DeptModel {DeptId=4,DeptName="研发部" },
                new DeptModel {DeptId=5,DeptName="项目部" },
                new DeptModel {DeptId=6,DeptName="市场部" },
                new DeptModel {DeptId=7,DeptName="测试部门" },
            };

    1、内连接(join)查询

            //Lambda写法
                var data = Emp.Join(Dept, e => e.DeptId, d => d.DeptId, (e, d) => new { e, d }).ToList();
                //Linq写法
                var data2 = (from e in Emp
                             join d in Dept
                             on e.DeptId   equals d.DeptId
                             select new { e, d }).ToList();

    2、左(left join )连接查询

           //Linq写法
                var data3 = (from e in Emp
                             join d in Dept
                             on e.DeptId equals d.DeptId into list
                             from dept in list
                             select new { e, dept }).ToList();
                //Lambda写法
                var data4 = Emp.GroupJoin(Dept, e => e.DeptId, d => d.DeptId, (e, d) => new { e, d = d.FirstOrDefault() }).ToList();

    3、交叉(Corss join)连接

            //Linq写法
                var data5 = (from e in Emp
                             from d in Dept  
                             select new { e, d }).ToList();
                //Lambda写法
                var data6 = Emp.SelectMany(emp => Dept.Select(dept => new { emp, dept })).ToList();

     Lambda

    Func<int, string> fun = (int a) => { return "返回值" + a; };
    Func<int, string> fun2 = delegate (int a) { return "返回值" + a; };
    
    Action action = () => { Console.WriteLine("返回值1"); };
    Action action2 = delegate () { Console.WriteLine("返回值2"); };
                 
    Predicate<int> predicate = delegate (int a) { return a > 10; };
    Predicate<int> predicate3 = (int a) => { return a > 10; };    
    View Code
  • 相关阅读:
    搭建Maven版SSM工程
    mac终端常用的命令
    常见的HTTP请求错误
    Go通关03:控制结构,if、for、switch逻辑语句
    Go通关14:参数传递中,值、引用及指针之间的区别
    Go通关13:究竟在什么情况下才使用指针?
    Go通关12:如何写出高效的并发模式?
    Go通关11:并发控制神器之Context深入浅出
    Go通关10:并发控制,同步原语 sync 包
    Go通关09:并发掌握,goroutine和channel声明与使用!
  • 原文地址:https://www.cnblogs.com/wwj1992/p/6756593.html
Copyright © 2011-2022 走看看