linq 左连接和有连接

linq 左连接写法  ,注意:这里主要第二个集合有可能为空。需要判断

1
2 var leftJoinSql = from student in  Student
3                   join book in  Book on student.ID equals book.StudentID into temp
4                   from tt in temp.DefaultIfEmpty()
5                   select new
6                    {
7                        sname= student.Name,
8                        bname = tt==null?"":tt.Name//这里主要第二个集合有可能为空。需要判断
9                    };

linq 有连接

1  2 var rightJoinSql = from book in Book
3                    join stu in Student on book.StudentID equals stu.ID into joinTemp
4                    from tmp in joinTemp.DefaultIfEmpty()
5                    select new { 
6                       sname=tmp==null?"":tmp.Name,
7                       bname=book.Name
8                     };

 

posted @ 2021-05-26 12:05  laoyang01  阅读(231)  评论(0编辑  收藏  举报