I have LINQ query that is built up in a piecemeal fashion like so:
var initialQuery = from item in MyContext where xxx == yyy select item;
var furtherQuery = from item in initialQuery where bla == foo select new { some stuff };
// more code here...
// eventually:
var yetAnotherQuery = (from item in furtherQuery ...)
.OrderBy(my_condition);
// As far as I know, the following query should still maintain the order of the previous one
// see: https://stackoverflow.com/questions/911942/are-subqueries-in-linqtosql-guaranteed-to-be-in-the-same-order-as-their-parent
var stillAnotherQuery = (from item in yetAnotherQuery
select item.data_I_care_about)
.Distinct();
// And finally...
var finalQuery = stillAnotherQuery.Skip(PageIndex).Take(PageSize);
But I am getting an exception when Skip()
is called, saying that the query is not ordered!
So apparently what is indicated in my code comment above and the referenced SO question is not entirely true. In fact another SO answer indicates that the preservation of that order is not guaranteed.
Does anyone know a good way to do what I am trying to accomplish?
I considered simply including a ROW_NUMBER
in my intermediate results, and ordering by that at the very end, but I cannot find a way to get that ROW_NUMBER
in my results via LINQ.
I have seen several other SO questions trying to get the ROW_NUMBER in there, but they are all clientside, as far as I have seen.
I seem to have painted myself in a corner. Anyone know a (LINQ-friendly) way out?
UPDATE
Some have suggested that I do the Distinct()
before the OrderBy()
.
I believe that would give me different results.
Imagine I have this table of data
myRank | myData
-------+--------
3 | A
1 | B
2 | A
Supposing I am ordering by myRank
, and the data I care about is myData
, and imagine my original code was like this:
var query = from item in MyTable
select item;
query = query.OrderBy(item => item.myRank);
var derivedQuery = from item in query // Note: we throw away myRank
select item.myData;
derivedQuery = derivedQuery.Distinct();
If I swap the order of the OrderBy()
and Distinct()
, I will get different results. I do not want myRank
to be included in the Distinct()
.
Sorry, this is part of a much larger process, so it is hard to get all the details into this question.
But hopefully that makes sense?