• 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
  • 相关阅读:
    指定的参数已超出有效值的范围。参数名:sit ,先仔细看看错误和我的一样不一样
    简单说下C#变量的作用域
    C#常用的字符串处理方法
    驼峰命名、帕斯卡命名、匈牙利命名--三种命名方法
    Python的安装
    Python下numpy的使用
    命名法:骆驼(Camel)、帕斯卡(pascal)、匈牙利(Hungarian)、下划线(_)
    PHP中高级面试问题集锦
    利用python,生成word
    python实现网页截图
  • 原文地址:https://www.cnblogs.com/wwj1992/p/6756593.html
Copyright © 2020-2023  润新知