EF 查询表达式 join

数据源:

 

1.无into,相当于 inner join

var query = from p in context.P
                            join s in context.S on p.PID equals s.PID
                            select new { p.PNAME, s.SNAME };
                query.ToList().ForEach(x =>
                {
                    Console.WriteLine("{0} - {1}", x.PNAME, x.SNAME);
                });
                Console.WriteLine("数量:" + query.Count());
                Console.ReadLine();

结果:

2.有 into,类似于 left join,注意,返回了主表多条对应的数据

var query = from p in context.P
                            join s in context.S on p.PID equals s.PID
                            into ab
                            from abTable in ab.DefaultIfEmpty()
                            select new { p.PNAME, abTable.SNAME };
                query.ToList().ForEach(x =>
                {
                    Console.WriteLine("{0} - {1}", x.PNAME, x.SNAME);
                });
                Console.WriteLine("数量:" + query.Count());

结果:

 

posted @ 2019-05-06 16:44  jasonlai2016  阅读(385)  评论(0)    收藏  举报