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

结果:

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