Take() takes different values than Skip() skips
c#, linq
Solution
the object with the same purchased value as this object is never shown
It did not click till I read this sentence a few times. Assuming you mean you have data like the following
class Videogames
{
public string Name { get; set; }
public int purchased { get; set; }
public string gamesystem { get; set; }
public Videogames(string name, int purchased)
{
Name = name;
this.purchased = purchased;
gamesystem = "PC";
}
}
static void Main(string[] args)
{
var VideoGames = new List<Videogames>();
VideoGames.Add(new Videogames("A", 1));
VideoGames.Add(new Videogames("B", 2));
VideoGames.Add(new Videogames("C", 2));
VideoGames.Add(new Videogames("D", 3));
var query = (from v in VideoGames
where v.gamesystem == "PC"
orderby v.purchased descending
select v).Take(2);
var query2 = (from v in VideoGames
where v.gamesystem == "PC"
orderby v.purchased descending
select v).Skip(2).Take(2);
}
You are getting results like `A, B` and `B, D`...
The problem is you do not have deterministic sorting, when there is a tie it is up to whatever underlying system is performing the `orderby` (likely SQL server, and this is EF or Similar).
To fix this you must make your sorting system more specific so there is no ambiguous ties for the sorting engine to decide for you.
Changing your queires to
var query = (from v in VideoGames
where v.gamesystem == "PC"
orderby v.purchased descending, v.Name ascending
select v).Take(2);
var query2 = (from v in VideoGames
where v.gamesystem == "PC"
orderby v.purchased descending, v.Name ascending
select v).Skip(2).Take(2);
would fix it. You did not show your model so I had to make up a field `Name`. In database situations you usualy have some kind of primary key `ID` field, just make your queries sort by the primary key as the last sorting parameter and you should be fine.
Problem
I have written two different queries, the first one is supposed to get the first 5 objects, and then the next one is supposed to get the next 5 objects ordered by the purchased value. The problem is that the two values in the middle are the same and when I take the first five objects, and then skip the first five objects and take the next five objects the last object of the first set is the same as the first value of the second set and the object with the same purchased value as this object is never shown. My queries are below. ``` var query = (from v in db.VideoGames where v.gamesystem == "PC" orderby v.purchased descending select v).Take(5); var query2 = (from v in db.VideoGames where v.gamesystem == "PC" orderby v.purchased descending select v).Skip(5).Take(5); ``` I would like to know if there is something that I can do differently to keep this from happening. Edit: I feel that my explanation may be a little confusing so I am going to add an example given 10 VideoGame objects in a database and there purchased value. - VideoGame1.purchased = 1, - VideoGame2.purchased = 2, - VideoGame3.purchased = 3, - VideoGame4.purchased = 4, - VideoGame5.purchased = 5, - VideoGame6.purchased = 5, - VideoGame7.purchased = 7, - VideoGame8.purchased = 8, - VideoGame9.purchased = 9, - VideoGame10.purchased = 10 Here is what I am receiving `query`: VideoGame10, VideoGame9, VideoGame8, VideoGame7, VideoGame5 `query2`: VideoGame5, VideoGame4, VideoGame3, VideoGame2, VideoGame1 Here is what I want `query`: VideoGame10, VideoGame9, VideoGame8, VideoGame7, VideoGame6 `query2`: VideoGame5, VideoGame4, VideoGame3, VideoGame2, VideoGame1 I do not care if I get VideoGame5 in the first query, just as long as I get both the VideoGame5 object and the VideoGame6 object.