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
  • 相关阅读:
    门面模式简述
    转:日志组件logback的介绍及配置使用方法
    spring boot项目中使用sfl4j+logbak配置
    druid相关资料
    spring boot +druid数据库连接池配置
    设计模式之Strategy模式
    转:高效代码审查的八条准则和十个经验
    SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?
    【其它】关于本博客的一些说明
    [THUWC2020] 自爆记
  • 原文地址:https://www.cnblogs.com/wwj1992/p/6756593.html
Copyright © 2011-2022 走看看