总结 jion,group join 基于方法的查询与查询表达式 对比

数据源:

代码:

using (tempdbEntities context = new tempdbEntities())
            {
                #region 基于方法的查询
                Console.WriteLine("基于方法的查询-Join:");
                var queryFF = context.P.Join(context.S, p => p.PID, s => s.PID, (p, s) => new { p.PNAME, s.SNAME });
                queryFF.ToList().ForEach(x => { Console.WriteLine("{0} - {1}", x.PNAME, x.SNAME); });
                Console.WriteLine("数量:" + queryFF.Count());

                Console.WriteLine("基于方法的查询-GroupJoin:");
                var queryFF2 = context.P.GroupJoin(context.S, p => p.PID, s => s.PID, (p, s) => new { p.PNAME, count = s.Count() });
                queryFF2.ToList().ForEach(x => { Console.WriteLine("{0} - {1}", x.PNAME, x.count); });
                Console.WriteLine("数量:" + queryFF2.Count());
                #endregion

                #region 查询表达式
                Console.WriteLine("查询表达式-无into:");
                var queryCX = from p in context.P join s in context.S on p.PID equals s.PID select new { p.PNAME, s.SNAME };
                queryCX.ToList().ForEach(x => { Console.WriteLine("{0} - {1}", x.PNAME, x.SNAME); });
                Console.WriteLine("数量:" + queryCX.Count());

                Console.WriteLine("查询表达式-有into:");
                var queryCX2 = from p in context.P join s in context.S on p.PID equals s.PID into ps from psTable in ps.DefaultIfEmpty() select new { p.PNAME, psTable.SNAME };
                queryCX2.ToList().ForEach(x => { Console.WriteLine("{0} - {1}", x.PNAME, x.SNAME); });
                Console.WriteLine("数量:" + queryCX2.Count());
                #endregion

                Console.ReadLine();
            }

结果:

 

posted @ 2019-05-07 09:02  jasonlai2016  阅读(133)  评论(0编辑  收藏  举报