Converting an SQL from Sql Server to C#LINQ

Asked

Viewed 103 times

3

I have an sql that looks like this:

SELECT coluna1, coluna2, coluna3
  FROM
  (
      SELECT coluna1, coluna2, coluna3, ROW_NUMBER() OVER(ORDER BY coluna1, coluna3 desc) as row
       FROM tabela1
      WHERE coluna4 in ('a', 'b', 'c', 'd')
  ) A
 WHERE
        A.ROW BETWEEN 1 AND 5
ORDER BY  A.ROW

And I want to pass this instruction to C’s LINQ expression#.

Note: I am using ROW_NUMBER OVER(...) to be able to identify the line number and be able to do something similar to what you have in ORACLE: SELECT ... FROM ... limit 1,5

1 answer

2


Your ROW_NUMBER() would be something equivalent to Top from what I understand. For this your query could use the Take which returns a specified number of contiguous elements from the beginning of a sequence.

Along with the Skip that ignores a specified number of elements in a sequence and returns the remaining elements.

So I try a pagination of your data.

var dados = (from t in tabela1
             where criterios....
            select new { Colunas....}).Skip(4).Take(5)
  • The ROW_NUMBER() that I’m wearing is similar to the TOP, but, I want for example: TOP 4, 5. What would it be, from the 4th record return the next 5.

  • Use the Skip together

  • Summarizing what you want to do is pagination.

  • That’s right. Paging.

  • Then you have to use Skip and take I just don’t know if the order is right there, once I get to the service I reissue the answer

  • I edited my reply complementing with the use of Skip.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.