总结 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();
            }

结果:

 

转载于:https://www.cnblogs.com/jasonlai2016/p/10823446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值