Example #1
0
        static void Main(string[] args)
        {
            #region
            //int[] comparValue = { 0, 0, 11, 11, 22, 11, 11, 22, 22 };

            ////方法1--加点!!!!
            //var tempV = from c in comparValue group c by c into g select new { value = g.Key, count = g.Count() };
            //var v1 = (from v in tempV orderby v.count descending select v.value).First();
            //Console.WriteLine(v1.ToString());

            ////方法2
            //var query = (from num in
            //                 (
            //                 from number in comparValue
            //                 group number by number into g
            //                 select new
            //                 {
            //                     number = g.Key,
            //                     cnt = g.Count()
            //                 }
            //             )
            //             orderby num.cnt descending
            //             select num.number).First();
            //Console.WriteLine("{0}", query);

            ////方法3
            //var res = from n in comparValue
            //          group n by n into g
            //          orderby g.Count() descending
            //          select g;
            //// 分组中第一个组就是重复最多的
            //var gr = res.First();
            //Console.WriteLine(gr.Key);
            #endregion

            //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();


            Console.ReadKey();
        }