Union order in Linq to Entities

12,502

Solution 1

It's because of the latter Union, you cannot guarantee order with it. You want to do this on your return:

return (p1.Union(p2).OrderByDescending(m => m.id));

Update

With further understanding of the issues, I think this will take care of it:

public IQueryable<photos> GetNextPrev(Int64 photoid, string userlogin)
{
    var p1 = (from m in db.photos
             where m.id < photoid && m.userlogin == userlogin
             orderby m.id descending
             select m).Take(2).Skip(0);
    var p2 = (from m in db.photos
              where m.id >= photoid && m.userlogin == userlogin
              orderby m.id 
              select m).Take(3).Skip(0);
    return (p1.Union(p2).OrderBy(m => m.id));
}

Solution 2

You can't assume any ordering. You always need an OrderBy if you want things ordered.

return p1.Union(p2).OrderBy(p=> p.id);
Share:
12,502
Evgeniy Labunskiy
Author by

Evgeniy Labunskiy

Project Manager at Ciklum

Updated on June 04, 2022

Comments

  • Evgeniy Labunskiy
    Evgeniy Labunskiy almost 2 years

    I have a problem with EDM model Union select. I have the records in db with uniqe Ids. For example id list: 1, 2, 3, 4, 5, 6, 7, 8, 9

    I need to select, for example, record #6 and 2 records before #6 and 2 records past #6. In select result it should be 4,5,6,7,8

    I made this next way:

    public IQueryable<photos> GetNextPrev(Int64 photoid, string userlogin)
        {
            var p1 = (from m in db.photos
                     where m.id < photoid && m.userlogin == userlogin
                     orderby m.id descending
                     select m).Take(2).Skip(0);
            var p2 = (from m in db.photos
                      where m.id >= photoid && m.userlogin == userlogin
                      orderby m.id descending
                      select m).Take(3).Skip(0);
            return (p1.Union(p2));
        }
    

    But the ordering is not like in the example...

    Thanks for the help!

  • Evgeniy Labunskiy
    Evgeniy Labunskiy about 13 years
    if i use your solution i have next one: select number 3 - result: 9,8,7,2,1; not 5,4,3,2,1.. :(
  • Evgeniy Labunskiy
    Evgeniy Labunskiy about 13 years
    thanks for your unswer but still not. now we have: I get #9 - the query show only number 9, but should 9,8,7. Maybe the link to project can help you to help me :). Thehe it is - test.wi-net.com.ua/Photos/test.
  • Ralph Shillington
    Ralph Shillington about 13 years
    @adrian, and additional problem with this proposed solution is the assumption that the ID values are contiguous. The union approach as per the OP is the surest bet.
  • Justin Morgan
    Justin Morgan about 13 years
    You left out a descending clause on your second orderby. Was that intentional?
  • Adriano Carneiro
    Adriano Carneiro about 13 years
    Yes! It should be ascending, because it will get the next 3.
  • Evgeniy Labunskiy
    Evgeniy Labunskiy about 13 years
    @adrian It works!! Thanks a lot! Like every time the solution is very simple :))