Как перевести расширенный оператор SQL с помощью LEFT OUTER JOIN в LINQ? - PullRequest
0 голосов
/ 09 октября 2019

У меня есть такой же оператор SQL, как показано в этом примере . Для следующей таблицы

CREATE TABLE [docs] (
  [id] int NOT NULL,
  [rev] int NOT NULL,
  [content] varchar(200) NOT NULL,
  PRIMARY KEY ([id],[rev])
) ;

и следующих данных

INSERT INTO [docs] ([id], [rev], [content]) VALUES
  (1, 1, 'The earth is flat'),
  (2, 1, 'One hundred angels can dance on the head of a pin'),
  (1, 2, 'The earth is flat and rests on a bull``s horn'),
  (1, 3, 'The earth is like a ball.');

оператор SQL

SELECT d1.*
    FROM docs AS d1
    LEFT OUTER JOIN docs AS d2
    ON (d1.id = d2.id AND d1.rev < d2.rev)
    WHERE d2.id is null
    ORDER BY id;

показывает только строки с максимальным значением rev для каждого id:

id  rev content
1   3   The earth is like a ball.
2   1   One hundred angels can dance on the head of a pin

Мой вопрос: Как мне перевести это утверждение в LINQ-to-SQL? Проблема, на мой взгляд, заключается в AND и < в предложении ON.

Ответы [ 2 ]

0 голосов
/ 11 октября 2019

В общем, лучше всего перевести SQL LEFT JOIN...WHERE ... = null в EXISTS, который в LINQ равен Any:

var ans = from d1 in docs
          join d2 in docs on d1.id equals d2.id into d2j
          where !d2j.Any(d2 => d1.rev < d2.rev)
          orderby d1.id
          select d1;

Но, конечно, вы можете перевести его в явныйТест LINQ null:

using (MyDataContext context = new MyDataContext()) {
    var query =
        from d1 in context.docs
        join d2 in context.docs on d1.id equals d2.id into d2j
        from d2 in d2j.Where(d2_2 => d1.rev < d2_2.rev).DefaultIfEmpty()
        where d2 == null
        select d1;
    return query.ToArray();
}
0 голосов
/ 10 октября 2019

Я пытался применить рецепт @ NetMage, но я застрял в состоянии <:

   using (MyDataContext context = new MyDataContext())
    {
        var query =
            from d1 in context.docs
            join d2 in context.docs on d1.id equals d2.id into jrv
            from x in jrv.Where(x => /* ??? */).DefaultIfEmpty()
            where x equals null
            select x;
        return query.ToArray();
    }

Лямбда-выражение в Где должно быть сравнение между d1.rev иd2.rev. Как я могу это сделать?

...