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 @ 2019-05-06 16:16  jasonlai2016  阅读(1130)  评论(0编辑  收藏  举报