Click here to Skip to main content
13,895,506 members
Rate this:
 
Please Sign up or sign in to vote.
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:
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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();
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
public List<SomeEntity> Paginate(int skip, int take, Expression<Func<SomeEntity, bool>> predicate)

compared to
.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.
   
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.
Ariel Quiroz 15-Jan-19 10:33am
   
Yes, I used debugger even I have printed these variables and always are changing.
Dave Kreskowiak 15-Jan-19 11:44am
   
OK, this is where YOU have to determine why the values you see in the debugger are showing up. Are they the same values on every pass through the method? Are they different values and the same page of data is being returned? Or is a different page of data being returned but the user still sees the same page?

This is stuff YOU have to dig into. Nobody is going to be able to do this for you or tell you what's wrong with it.
Ariel Quiroz 15-Jan-19 14:29pm
   
Are they the same values on every pass through the method? No, they are changing. I am implementation a webApi then from my endpoint I call this method.
The data is not changing but Yes the variables skipValue and takeValue.
I am new in EntityFramework and netCore, but I think the error is in my Context or my DbSet. Because this way it is working.
var res =_context.SomeEntity.Where(predicate)
                            .OrderByDescending(t => t.DateCreate)            
                            .ToList();   
return res.Skip(skipValue).Take(takeValue).ToList();
Dave Kreskowiak 15-Jan-19 14:52pm
   
The error is not going to be in the Context or the Entity class code.

Is the being returned changing? Or is it just being ignored by your client-side code to update the display?

To find out, do this in the code you just posted:
var res =_context.SomeEntity
    .Where(predicate)
    .OrderByDescending(t => t.DateCreate)
    .Skip(skipValue)
    .Take(takeValue)
    .ToList();

return res;

Put a breakpoint on the return line and run the code. Hover the mouse over the res variable when the debugger breaks to see the data being returned to the client. If it is not the page it should be, you've got to hover the mouse over the skipValue and takeValue variables to see their content and trace back through the code back to the client to figure out why those values are being passed in.

If the values are good and the data being returned in good, then you have to look at the client code that is receiving the data and figure out why it's not using the returned data.

Nobody here can do this for you because we can't see any of your code other than what you post here. We also don't have your data, database, input into the app, ... nothing.

We can do none of this for you.
Ariel Quiroz 16-Jan-19 7:51am
   
Obviously, I put a lot of breakpoints.
A example could be the following.
My code is this:
1 var res =_context.SomeEntity
2                  .Where(predicate)
3                  .OrderByDescending(t => t.DateCreate)
4                  .Skip(skipValue)
5                  .Take(takeValue)
6                  .ToList();
7
8     return res;


Fist time:
Input:
Data: A, B, C, D, E, F, G, H, I, J.
skipValue = 0
takeValue = 2
Output:
res = A, B

Second time:
Data: A, B, C, D, E, F, G, H, I, J.
skipValue = 3
takeValue = 2
Output:
res = A, B

Third time:
Data: A, B, C, D, E, F, G, H, I, J.
skipValue = 5
takeValue = 2
Output:
res = A, B

I hope you can understand me.
Thank you for your help.
Dave Kreskowiak 16-Jan-19 11:17am
   
That query works. I just tried it against my own database and it worked as expected. I HIGHLY suggest you create a small test app to test out your context. Something like this:
        static void Main(string[] args)
        {
            int skip = 0;
            int take = 5;

            for (skip = 0; skip <= 20; skip+=take)
            {
                var table = GetPage(skip, take);
                DumpTablePage(skip, take, table);
            }

            Console.WriteLine("Press ENTER to quit...");
            Console.ReadLine();
        }

        static void DumpTablePage(int skipValue, int takeValue, IEnumerable<MyEntityClass> tablePage)
        {
            Console.WriteLine($"Page starts at {skipValue} and runs for {takeValue} records:");
            foreach (MyEntityClass item in tablePage)
            {
                Console.WriteLine($"\t{item.NameProperty}");
            }
        }

        static IEnumerable<MyEntityClass> GetPage(int skipValue, int takeValue)
        {
            using (MyContextClass context = new MyContextClass())
            {
                // Setup the SQL query.
                var query = context.MyEntityClass
                            .Where(x => x.Status != "Retired")
                            .OrderByDescending(x => x.Id)
                            .Skip(skipValue)
                            .Take(takeValue);

                // Execute the query.
                var result = query.ToList();

                // Return the data.
                return result;
            }
        }

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190306.1 | Last Updated 15 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100