What would Take and Skip be in Linq?

Asked

Viewed 152 times

4

I am doing maintenance on a system and found a GetAllQueryable(), using the method Take() and Skip(). Well, I don’t know what they are or what they’re for, I looked them up but I couldn’t find anything in the documentation. Would anyone know what they’re for?

  • 1

    Edited, I’m sorry I.

  • 1

    Is it clear how they work and what they are for? Because they are great resources for certain types of queries, and it is important to understand them to use them. Any doubt comment on the answer that as soon as possible I try to clarify.

  • Got show beast! There is no way not understand with an explanation of these... Thank you very much!

1 answer

5


There are two query filters LINQ or Lambda Expressions which has its SQL equivalents, in case they are executed on a ORM, as in your case Nhibernate:

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.

Use:

// 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

Equivalent SQL query (Firebird 2.1):

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

Skip:

Ignore a specified number of elements in a sequence and return the remaining elements.

Use:

// 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

Equivalent SQL query (Firebird 2.1):

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

Joint use:

// 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

Equivalent SQL query (Firebird 2.1):

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

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

Browser other questions tagged

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