Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm implementing a function to paginate. I'm using the operators skip and take to achieve this goal.
My problem is the following:
I have created a function to paginate. For example:
C#
public List<SomeEntity> Paginate(int skipValue, int takeValue, Expression<Func<SomeEntity, bool>> predicate)
{
    _context.SomeEntity
            .Where(predicate)
            .OrderByDescending(t => t.DateCreate)
            .Skip(skipValue)
            .Take(takeValue)
            .ToList();
}

The first time skip and take are working but when i call again to this function but with distinct values for skip and take, the function does not respect these values, it is taking the previous values.

Just in case I have created an web application. Then my Controller call this function.

Somebody know because is caching the values from skip and take??

What I have tried:

I don't know because is happening this error.
Posted
Updated 15-Jan-19 4:38am
v2
Comments
F-ES Sitecore 15-Jan-19 10:51am    
Skip and Take *do* work, so the issue is elsewhere in your code. You're either not passing the right values in to the function, or passing values different from what you think you are, or the problem could be with a different function. You're going to have to use the debugger to get a better idea what is happening. Also use SQL Trace to examine the actual SQL being executed, that might give you a better idea about what is happening.

I have resolved this way:

public List<SomeEntity> Paginate(int skipValue, int takeValue, 
                                 Expression<Func<SomeEntity, bool>> predicate)
{
  var res =_context.SomeEntity
            .Where(predicate)
            .OrderByDescending(t => t.DateCreate)
            .ToList();

   return res.Skip(skipValue).Take(takeValue).ToList();  
}


I don't like this solution. I don't understand because it is not working this way:

_context.SomeEntity
            .Where(predicate)
            .OrderByDescending(t => t.DateCreate)
            .Skip(skipValue)
            .Take(takeValue)
            .ToList();
 
Share this answer
 
Look at your Skip and Take code and compare the variable names used in those calls with the variable names passed into the Paginate method header.
C#
public List<SomeEntity> Paginate(int skip, int take, Expression<Func<SomeEntity, bool>> predicate)

compared to
C#
.Skip(skipValue)
.Take(takeValue)

Since, I'm assuming, the code compiles, that would lead me to believe you've got class-level variables called skipValue and takeValue. I don't know how your code is written, but it seems like having those variables as class-level would be a bad idea.

By the way, using the debugger on this code would have made it very easy to figure out where the problem is.
 
Share this answer
 
v2
Comments
Ariel Quiroz 12-Jan-19 20:40pm    
Sorry, I have written bad. The name of variables are the same.
Ariel Quiroz 12-Jan-19 20:41pm    
public List<someentity> Paginate(int skipValue, int takeValue, Expression<Func<SomeEntity, bool>> predicate)
{
_context.SomeEntity
.Where(predicate)
.OrderByDescending(t => t.DateCreate)
.Skip(skipValue)
.Take(takeValue)
.ToList();
}
Dave Kreskowiak 13-Jan-19 17:58pm    
Again, you're going to have to use the debugger and step through this code to see the values that it is getting.
Richard Deeming 18-Jan-19 11:11am    
Where's the return keyword in that function?
Dave Kreskowiak 18-Jan-19 15:58pm    
I think he's paring down the code to nothing to make it harder to answer his question, or to save on internet costs because he's billed by the character he posts.

The code won't even compile if the return statement is missing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900