我在nHibernate中有以下查询。这个想法是获得某个数据组的第一个和最后一个测量时间。
var measurements = _session.Query<Measurement>()
.Where(x => categories.Contains(x.CategoryId));
first = measurements.Min(o => o.StartTime);
last = measurements.Max(o => o.StartTime);
SQL Server Profiler提供以下输出:
exec sp_executesql N'select cast(min(measuremen0_.StartTime) as DATETIME) as col_0_0_ from Measurement measuremen0_ where measuremen0_.Category in (@p0 , @p1)',N'@p0 int,@p1 int',@p0=7654321,@p1=3324673
exec sp_executesql N'select cast(max(measuremen0_.StartTime) as DATETIME) as col_0_0_ from Measurement measuremen0_ where measuremen0_.Category in (@p0 , @p1)',N'@p0 int,@p1 int',@p0=7654321,@p1=3324673
我可以以某种方式优化此而不使用HQL ,以便它只会向数据库服务器创建一个请求吗?