EF中 GroupJoin 与 Join

时间:2019-05-06 16:56:42   收藏:0   阅读:459

数据:

技术分享图片

GroupJoin: 返回左表所有数据

using (tempdbEntities context = new tempdbEntities())
            {
                var query = context.P.GroupJoin(context.S, p => p.PID, s => s.PID,
                    (p, s) => new { p.PNAME, c = s.Count() });
                foreach (var obj in query)
                {
                    Console.WriteLine(
                        "{0} - {1}",
                        obj.PNAME,
                        obj.c
                        );
                }
                Console.WriteLine("数量:" + query.Count());
                Console.ReadLine();
            }

结果:

技术分享图片

Join:返回交集

using (tempdbEntities context = new tempdbEntities())
            {
                var query = context.P.Join(context.S, p => p.PID, s => s.PID,
                    (p, s) => new { p.PNAME, s.SNAME});
                foreach (var obj in query)
                {
                    Console.WriteLine(
                        "{0} - {1}",
                        obj.PNAME,
                        obj.SNAME
                        );
                }
                Console.WriteLine("数量:" + query.Count());
                Console.ReadLine();
            }

结果:

技术分享图片

 

原文:https://www.cnblogs.com/jasonlai2016/p/10820311.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!