How to access next and previous record with C# using Entity Framework

Asked

Viewed 1,573 times

7

I am developing a Windows Form application, with Entity Framework 6 + Mysql.

As is common in systems, I have in each form navigation buttons (First Record, Previous Record, Next Record, Last Record)

The doubt: there is some defined syntax (LINQ or Lambda), which takes me directly to the previous or next EXISTING record?

Example: I have the form displaying the record ID = 6. If I click the previous button, I should display the record 5. Likewise, the next one is record 7. However, if record 7 does not exist, record 8 should be displayed.

I can easily solve with programming logic, but I would like to know if there is a native language way to solve this type of operation.

Thank you.

  • 1

    You will use this function right after you save the record, or you can "browse" through other actions before?

  • @Randrade, I will use to navigate between the options, for example go to the next record, and return.

2 answers

6

You can use the following:

For item before ID 6:

Context.NomeEntidades.where(c=>c.ID<6).OrderByDescending(c=>c.ID).firstOrDefault();

And to the next:

Context.NomeEntidades.where(c=>c.ID>6).OrderBy(c=>c.ID).firstOrDefault();
  • Thanks @Rebecaabrantes, it helps me a lot!

6


The most elegant way I know is used to create pagination in ASP.NET MVC applications, using Skip() and Take():

var registroAtual = 6;
var registroAnterior = contexto.Entidade.Skip(registroAtual - 2).Take(1).FirstOrDefault();
var proximoRegistro = contexto.Entidade.Skip(registroAtual).Take(1).FirstOrDefault();

EDIT

Suppose I want to specifically find the records before and next to Id 6:

var registroAnterior = contexto.Entidade.Where(e => e.Id <= 6).OrderByDescending(e => e.Id).Skip(1).Take(1).FirstOrDefault();
var proximoRegistro = contexto.Entidade.Where(e => e.Id >= 6).OrderBy(e => e.Id).Skip(1).Take(1).FirstOrDefault();
  • 1

    Thank you @Ciganomorrisonmendez.

  • I think it doesn’t work for the question... we can’t be sure that there are 5 records before registration just because the ID is 6. One or more (or all) records may have been removed.

  • 1

    @Rsinohara You possibly didn’t understand the code. Skip doesn’t look any id. It simply skips X records. Take picks up the X first records after Skip and return a list, no matter how many times Take(1). Finally, FirstOrDefault() takes the first element from the list if any, or returns null if there is no "next element". Test and check.

  • 1

    Yes, but maybe there are only two entries before the one with ID 6, say with ID 3 and 5. Giving Skip on 4 records will skip 3, 5, 6 and, say, 7, returning the next ID. Will not return the previous input (in case it would be ID 5).

  • 1

    @Rsinohara Yes, in fact you didn’t understand. Skip and Take do not look at Ids, nor order of Ids, and are the ones who work right with Guids. I could expand the selection by doing var proximoRegistro = contexto.Entidade.Where(e => e.Id == 6).Skip(registroAtual).Take(1).FirstOrDefault();. Would have done the same.

  • 1

    @Gypsy omorrisonmendez Your code is correct, but for pagination. The AP asked to find the Element prior to the ID 6. Skipping 4 elements does not return the previous and following element of ID 6 (unless all elements are present with Ids from 1 to 5). Mentally run your code to a table with only the elements with Ids: 1, 5, 6, 1890, 1891, 5000, 12000 and 14000. He will return: registroAnterior with ID 1891 and proximoregistro with ID 12000.

  • 1

    @Rsinohara See now.

  • @I think it’s worse now: the Where clause returns only 1 item (considering it exists). Skip in this eliminates the single item, zeroing the collection.

  • Maybe to use Skip we’d have to do contexto.Entidade.OrderBy(e=>e.ID).Skip(contexto.Entidade.Where(e=>e.ID<registroAtual).Count()-1).FirstorDefault but will give error if there are no entries.

  • @Rsinohara Never mind.

Show 5 more comments

Browser other questions tagged

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