1

この SQL クエリを linq に書き込む方法を知っていますか?

SELECT * 
FROM 
  a 

  INNER JOIN b 
  ON a.FkSubmissionId = b.Id 

  RIGHT JOIN c 
  ON a.FkItemId = c.Id 

WHERE 
 (b.FkUserId = '...' OR b.FkUserId is null) 
 and 
 (c.FkTenderId = 2)

私はLinquerを使用していますが、ツールから得た最高のものは次のとおりです。

リンク:

from 
  items in _context.Items 

from 
  si in _context.si

  join s in _context.s 
  on new { fki = si.fki } equals new { fki = s.Id } 

into 
  submissions_join

from 
  s in submissions_join.DefaultIfEmpty()

...

SQL の結果:

SELECT * 
FROM 
  [Items] AS [t0]

  CROSS JOIN [SubmissionsItems] AS [t1]

  LEFT OUTER JOIN [Submissions] AS [t2] 
  ON [t1].[FkSubmissionId] = [t2].[Id]

WHERE 
  (([t2].[FkUserId] = @p0) OR (([t2].[FkUserId]) IS NULL)) 
  AND 
  ([t0].[FkTenderId] = @p1)

したがって、最終結果は、必要なクエリから取得したものではありません...

ご協力ありがとうございました !!!

4

2 に答える 2

1

これを試して:

var part1 =
    from x in a
    join y in b on x.FkSubmissionId equals y.Id
    where b.FkUserId = "..."
    select new {x, y};

var part2 =
    from c in z
    where c.FkTenderId == 2
    join xy in part1
        on z.Id equals xy.x.FkItemId
        into xys
    from xy in xys.DefaultIfEmpty()
    select new {xy.x, xy.y, z};
于 2012-09-17T13:44:37.973 に答える
0

右結合の代わりに左結合を使用できるように、クエリを並べ替えます

var query = from c in context.C
            from a in context.A.Where(x => c.Id == x.FkItemId)
                               .DefaultIfEmpty()
            join b in context.B on a.FkSubmissionId equals b.id
            where b.FkUserId == '...' || b.FkUserId == null
            where c.FkTenderId == 2
            select new {
              a,
              b,
              c
            };
于 2012-09-17T14:03:21.147 に答える