实体框架 - 左连接和OR条件

时间:2013-10-17 11:35:36

标签: sql entity-framework

我需要在实体框架中进行查询但是不起作用。我正在使用join,left foint和=。     我试着这样做,但它不起作用。当我在Sqlserver中运行查询时,它返回2行,但是按实体框架返回0行

The query is :
SELECT *
FROM   DATABASE..PENDS P
       INNER JOIN DATABASE..VIEWERS V
               ON P.TIPO = V.TIPO
       LEFT JOIN DATABASE..GROUP G
              ON V.GRUPO = G.ID
       INNER JOIN DATABASE..AUTH A
               ON (A.AUT_LOGIN = V.USUARIO OR A.GRUPO = G.ID)
WHERE  A.AUT_LOGIN = 'userlogin' 

I tryed to do this way:

var pendsc = (from p in _context.Pends
                              join v in _context.Viewers on new
                              {

                                  p.Tipo
                              }
                              equals new
                              {
                                        v.Tipo
                              }
                              join g in _context.Group on new
                              {
                                               Grupo = v.Grupo
                              }
                              equals new
                              {

                                  Grupo = g.Id

                              }
                              from a in _context.Auth
                              where (a.Login == v.Usuario || a.Grupo == g.Id)
                              where a.Login == "userlogin"
                              select p).ToList();

1 个答案:

答案 0 :(得分:0)

对于左连接,您应该使用into clause with DefaultIfEmpty

var pendsc = (from p in _context.Pends
                              join v in _context.Viewers on
                             p.Tipo equals v.Tipo

                              join g in _context.Group on v.Grupo
                                                            equals g.Id into TippoGroup

                              from Obj in TippoGroup.DefaultIfEmpty()
                              join
                              a in _context.Auth
                              where (a.Login == v.Usuario || a.Grupo == g.Id)
                              where a.Login == "userlogin"
                              select p).ToList();