What would Take and Skip in Linq?

4

I'm doing maintenance on a system and found a GetAllQueryable() , which uses the Take() and Skip() method. Well, I do not know what they are, nor what they are for, I researched but I did not find anything in the documentation. Would anyone know what it's for?

    
asked by anonymous 17.06.2015 / 15:59

1 answer

5

Two query filters are LINQ or #

Consider this table in the examples:

ID | NOME
---------------------------------------------
1  | Cliente 1
2  | Cliente 2
3  | Cliente 3
4  | Cliente 4
5  | Cliente 5
6  | Cliente 6
7  | Cliente 7
8  | Cliente 8
9  | Cliente 9
10 | Cliente 10
11 | Cliente 11
12 | Cliente 12

Take

Returns a specified number of adjacent elements from the beginning of a sequence.

Usage:

// retorna os 3 primeiros elemento da tabela
var r = Session.QueryOver<Cliente>().Take(3).List();

Output:

ID | NOME
---------------------------------------------
1  | Cliente 1
2  | Cliente 2
3  | Cliente 3

SQL equivalent query (Firebird 2.1):

SELECT first 3 CL.ID, CL.NOME FROM CLIENTE CL

Skip : >

Ignores a specified number of elements in a sequence and returns the remaining elements.

Usage:

// retorna todos os items apartir do 2° elemento da tabela
var r = Session.QueryOver<Cliente>().Skip(2).List();

Output:

ID | NOME
---------------------------------------------
3  | Cliente 3
4  | Cliente 4
5  | Cliente 5
6  | Cliente 6
7  | Cliente 7
8  | Cliente 8
9  | Cliente 9
10 | Cliente 10
11 | Cliente 11
12 | Cliente 12

SQL equivalent query (Firebird 2.1):

SELECT skip 2 CL.ID, CL.NOME FROM CLIENTE CL

Use Together:

// retorna os 3(take) primeiros elementos apartir do 2°(skip) elemento da tabela
var r = Session.QueryOver<Cliente>().Take(3).Skip(2).List();

Output:

ID | NOME
---------------------------------------------
3  | Cliente 3
4  | Cliente 4
5  | Cliente 5

SQL equivalent query (Firebird 2.1):

SELECT first 3 skip 2 CL.ID, CL.NOME FROM CLIENTE CL
  

These techniques are used constantly, for pagination of queries, bringing better performance to the application. Where only the desired lines are obtained from the database.

    
17.06.2015 / 16:34