public class City
{
virtual public long Id { get; set; }
virtual public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
城市表包含重复的名称,我想删除重复项.我也想通过Id订购结果.
首先我想到了以下查询.
select distinct Name from City order by Id;
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
So my question is how can I do this query with QueryOver?
小智 5
这在ICriteria中是可能的:
var list =
session.CreateCriteria<City>()
.SetProjection(Projections.Group("Name"))
.AddOrder(Order.Asc(Projections.Min("Id")))
.List<string>();
Run Code Online (Sandbox Code Playgroud)
但是目前在QueryOver中不可能,因为缺少.OrderBy(IProjection)重载.一旦添加了缺失的重载,它应该看起来像:
var list =
s.QueryOver<City>()
.Select(Projections.Group<City>(p => p.Name))
.OrderBy(Projections.Min<City>(c => c.Id)).Asc
.List<string>();
Run Code Online (Sandbox Code Playgroud)
请注意,Projections重载现在就在那里,因此您可以在ICriteria中编写以下(类型安全)查询:
var list =
session.CreateCriteria<City>()
.SetProjection(Projections.Group<City>(c => c.Name))
.AddOrder(Order.Asc(Projections.Min<City>(c => c.Id)))
.List<string>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4129 次 |
| 最近记录: |