夜微凉、的博客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

首先定义几个模型类:  

 

   /// <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

 

posted on 2017-04-24 14:05  夜、微凉  阅读(330)  评论(0编辑  收藏  举报