views:

98

answers:

1
public class City
{
    virtual public long Id { get; set; }
    virtual public string Name { get; set; }
}

City table contains duplicated Names and I want to remove duplicates. I also want the results to be ordered by Id.

First I thought about the following query.

select distinct Name from City order by Id;

But this breaks with 'ORDER BY items must appear in the select list if SELECT DISTINCT is specified.' exception. After seeing http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx I think I should do:

select Name from City group by Name order by min(Id)

So my question is how can I do this query with QueryOver?

A: 

This is possible in ICriteria:

var list =
    session.CreateCriteria<City>()
        .SetProjection(Projections.Group("Name"))
        .AddOrder(Order.Asc(Projections.Min("Id")))
        .List<string>();

But this is not currently possible in QueryOver, because the .OrderBy(IProjection) overload is missing. Once the missing overload has been added it should look something like:

var list =
    s.QueryOver<City>()
        .Select(Projections.Group<City>(p => p.Name))
        .OrderBy(Projections.Min<City>(c => c.Id)).Asc
        .List<string>();

Note that the Projections overloads are there just now, so you can write the following (typesafe) query in ICriteria:

var list =
    session.CreateCriteria<City>()
        .SetProjection(Projections.Group<City>(c => c.Name))
        .AddOrder(Order.Asc(Projections.Min<City>(c => c.Id)))
        .List<string>();
FlukeFan