Skip to content

NH-2547 - Ordering by the same column more than once throws an error #726

@nhibernate-bot

Description

@nhibernate-bot

ryanhaugh created an issue — 23rd February 2011, 13:49:44:

I'm using MVC and have an action that returns different action results based on a given parameter (for example it may return a web page or excel document). The same IQueryable object will be used regardless of the output type. When an excel document is returned, the IQueryable object is simply given to the excel file generator. When a web page is returned, the IQueryable will be passed to the view where a third-party grid uses the IQueryable object to display the data.

The problem is that I want the object to already be sorted by the time it gets to the excel file generator so I add an OrderBy when generating the query (I don't want to have to deal with adding specific column ordering in the action because I would need to do it for every action that I use this and I want a generic solution). However, the third-party grid has built-in sorting capabilities and when I choose to sort by the column that was used in the OrderBy when creating the query I get an error saying the same column name appears more than once in the order by clause. Since the grid control can also handle paging I don't want to simply return a List or something and lose the IQueryable object.

It would be nice if the Linq provider removed the first instance of a column in the order by if the same column is added again.

I'm using MS SQL Server 2005, .NET 4.0.


Ricardo Peres added a comment — 2nd September 2014, 22:35:54:

I don't think this can/should be fixed... IMO, adding two order by clauses is an error


Alexander Zaytsev added a comment — 3rd September 2014, 1:49:57:

I think that the case is valid, but I'm lowering the priority


Alexander Zaytsev added a comment — 3rd September 2014, 1:50:07:

Also, what exception is thrown?


Oskar Berggren added a comment — 7th September 2014, 14:35:34:

Shouldn't a Linq OrderBy() simply(?) override any previous OrderBy() provided no skip/take or similar is between them?


Alexander Zaytsev added a comment — 7th September 2014, 21:53:08:

It should.


Ricardo Peres added a comment — 7th September 2014, 22:19:11:

In Entity Framework the last call to OrderBy is the one that is applied. No complaints about multiple ones.


Alexander Zaytsev added a comment — 7th September 2014, 23:09:17:

It is much more complicated, when ThenBy involved. As OrderBy/ThenBy do stable sorting.

So the following test passes:

var list = new List<T>();
for (int i = 0; i < 100; i<ins></ins>)
{
    list.Add(new T(r.Next(1, 10), r.Next(1, 10), r.Next(1, 10)));
}

var cs = list.OrderBy(x => x.B)
    .OrderBy(x => x.A)
    .ThenBy(x => x.A)
    .ThenBy(x => x.C)
    .ThenBy(x => x.A)
    .ThenBy(x => x.B)
    .OrderBy(x => x.A)
    .ToList();

var cs2 = list.OrderBy(x => x.A)
    .ThenBy(x => x.C)
    .ThenBy(x => x.B);

Assert.That(cs, Is.Ordered.By("A"));
Assert.True(cs.SequenceEqual(cs2));

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions