doubts with entity and limit

1

I need to search the last 100 records of the bank, I am using the following code, but it returns the first 100:

 dynamic data = null;
        try
        {
            data = limit == 0 ?
                (from p in contexto.Set<TEntity>() select p).Where(predicate).ToList() :
                (from p in contexto.Set<TEntity>() select p).Where(predicate).ToList().Take(limit);

        }

I tried to put the .Last (), but gave an error:

How can I resolve this?

    
asked by anonymous 12.09.2016 / 17:16

1 answer

0

Error:

A% conversion error is happening with List<TEntity> , they are different and conversion is required. The TEntity returns the last element of the list , and actually needs the last 100 elements , did you see the difference?

Fact:

I'm searching for the last 100 Bank records (% with%)

Then:

Sort your SQL by the key field ( OrderByDescending ) bring the 100 first records that actually are the last 100 logs .

var lista = (from p in contexto.Set<TEntity>() select p)
             .Where(predicate)
             .OrderByDescending(orderByPredicate)
             .Take(limit)
             .ToList();
When you put Last() and then ORDER BY CAMPO DESC it brought all the records and then got 100, this is a big mistake / em> , make ToList() only bring 100 records as shown in the example.

After that, if you need to Order in Take(100) of SQL , as you have already brought the last 100 memory do so (any filter, ordernation, etc.):

lista.OrderBy(predicate);
    
12.09.2016 / 17:21