EF中 GroupJoin 与 Join

数据:

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();
            }
复制代码

结果:

 

posted @   jasonlai2016  阅读(1145)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示