Reputation: 8124
I have a question about the internal behavior of Skip and Take. Do subsequent calls to skip and take move an internal pointer forward?
So, in other words, is:
var rowset = entities.ActivityRecords
.Where(ar => ar.State == (int)LineItemStates.Created
&& ar.Timestamp >= cycleIteration.StartDate //within the date range
&& ar.Timestamp <= cycleIteration.EndDate
&& matchingProducts.Contains(ar.ProductId))
.OrderBy(ar2 => ar2.Id);
rowset.Skip(10); //only works if we're consuming an iterator and the pointer is moving forward. Does this move an internal pointer forward?
return rowset.Take(10);
equivalent to:
var rowset = entities.ActivityRecords
.Where(ar => ar.State == (int)LineItemStates.Created
&& ar.Timestamp >= cycleIteration.StartDate //within the date range
&& ar.Timestamp <= cycleIteration.EndDate
&& matchingProducts.Contains(ar.ProductId))
.OrderBy(ar2 => ar2.Id);
return rowset.Skip(10).Take(10);
I have a bug I'm investigating, and I believe the problem is that the r.Take(x) is in a loop where the intent is to keep taking x
rows with each loop execution and thus consume the entire set, but I'm guessing the latter code snippet is required over the former as I don't think Skip and Take are really moving any internal pointer iterator forward.
Upvotes: 2
Views: 155
Reputation: 726599
rowSet.Take(x)
is in a loop where the intent is to keep taking x rows with each loop execution and thus consume the entire set
You are right, this bug is caused by a misunderstanding. You cannot repeatedly call Take
on the same rowSet
and expect different results. Since the query is evaluated lazily, each call would get you the same ten rows again and again, and your loop would not stop.
Upvotes: 2