LINQ - How do I organize my (complex) results?

I have a LINQ query that is created in parts like this:

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: /questions/1086729/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 get an exception when called Skip(), saying the request is not ordered! So it’s obvious that what is stated in my comment on the code above and the link to the SO question is not entirely true. In fact, another SO answer indicates that keeping this order is not guaranteed.

Does anyone know a good way to do what I'm trying to accomplish?

I thought that I simply included a ROW_NUMBERin my intermediate results and ordering it at the very end, but I cannot find a way to get this ROW_NUMBERin my results through LINQ.

SO questions, ROW_NUMBER, , .

, . - (LINQ-friendly) ?


UPDATE


, Distinct() OrderBy(). , .

, .

myRank | myData
-------+--------
   3   |    A
   1   |    B
   2   |    A

, myRank, , , myData, , :

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();

OrderBy() Distinct(), . , myRank Distinct().

, , .

, , ?

+5
4

, .

, , Distinct() a IQueryable<T>, OrderBy() IOrderedQueryable<TSource>, (-) EF

Distinct() OrderBy()

- :

var query = from item in MyTable
        select item;

query = query.GroupBy(item => item.myData, item => item.myRank);
var derivedQuery = query.OrderBy(group => group.Min())
                        .Select(group.Key);

:

  • , dinstinct (myData)
  • Min() myRank , ( ) myRank , Distinc()
+2

- Distinct .. 't Skip - , , Skip.

OrderBy Distinct. OrderBy, , . yetAnotherQuery stillAnotherQuery , .

, . yetAnotherQuery, Distinct OrderBy stillAnotherQuery:

var yetAnotherQuery = (from item in furtherQuery ...)
                      .OrderBy(my_condition);

var stillAnotherQuery = (from item in yetAnotherQuery ...)
                        .Distinct()
                        .OrderBy(my_condition);

var finalQuery = stillAnotherQuery.Skip(PageIndex).Take(PageSize);

... , :

var yaq_UnSorted = (from item in furtherQuery ...);

var yetAnotherQuery = yaq_UnSorted
                      .OrderBy(my_condition);

var stillAnotherQuery = (from item in yaq_UnSorted ...)
                        .Distinct()
                        .OrderBy(my_condition);

var finalQuery = stillAnotherQuery.Skip(PageIndex).Take(PageSize);

, . , .

Distinct/OrderBy - , . .

+2

OrderBy Distinct?

, Distinct .

: http://programminglinq.com/blogs/marcorusso/archive/2008/07/20/use-of-distinct-and-orderby-in-linq.aspx

. , GroupBy Distinct.

- :

var query = MyTable
    .OrderBy(item => item.myRank)
    .GroupBy(item => item.myData)
    .Select(grouping => grouping.First().myData);
+1
source

I don’t think the problem is that it Distinct()is reordering - but it returns “normal” IQueryable<T>instead IOrderedQueryable<T>, which is apparently needed to implement Linq to Entities Skip.

One solution might be to reorder the methods OrderBy()and Distinct(), therefore, execute first Distinct(), then apply the order later, and then use Skip().

0
source

All Articles