var users =
MvcApplication.SessionFactory.GetCurrentSession()
.QueryOver<User>().Fetch(x=>x.Roles).Eager;
if (!_userSession.IsUserInRole(UserRoles.Superuser))
users = users.Where(x => x.Company == user.Company);
int count =
users.Select(Projections.Count("id")).UniqueResult<int>();
var pagedUsers = users.OrderBy(x =>
x.LastName).Asc.Skip(page * pageSize).Take(pageSize).List();
to which i receive this error
Column "Users.LastName" is invalid in the ORDER BY clause because it
is not contained in either an aggregate function or the GROUP BY
clause.
Now in linq this runs too queries but it looks like QueryOver doesn't
return the instance, but modifies the existing one. Is this correct?
Do i need to repeat some logic to get the count as well?
Adam
The short answer is 'yes'. QueryOver behaves the same as ICriteria in this
example ... adding conditions to the query changes the query itself.
ICriteria has a corresponding CriteriaTransformer class which can be used to
clone the criteria for exactly this sort of thing. I'll have a think and
see if we need a QueryOverTransformer class to do something similar.
IQueryOver does allow you access to the underlying ICriteria, so one option
might be to do something like this:
var users =
mySession.QueryOver<User>()
...
.OrderBy(x => x.LastName).Asc
.Skip(page * pageSize)
.Take(pageSize);
var pagedUsers = users.List();
var count =
CriteriaTransformer.Clone(users.UnderlyingCriteria)
.ClearOrders()
.SetProjection(Projections.RowCount())
.UniqueResult<int>();
Hope that helps, and I'll see about getting a QueryOverTransformer (and
probably IQueryOver.ClearOrders()) added to NH.
Richard
--------------------------------------------------
From: "Schotime" <adamsc...@gmail.com>
Sent: Friday, February 12, 2010 9:45 PM
To: "nhusers" <nhu...@googlegroups.com>
Subject: [nhusers] QueryOver issues
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
Thanks for the reply.
Adam
On Feb 13, 9:11 pm, "Richard Brown \(gmail\)"
> From: "Schotime" <adamschro...@gmail.com>
There's now a QueryOverTransformer in the trunk code, so you can write:
var usersQuery =
mySession.QueryOver<User>()
...
.OrderBy(x => x.LastName).Asc
.Skip(page * pageSize)
.Take(pageSize);
var pagedUsers = usersQuery.List();
var count =
QueryOverTransformer.TransformToRowCount(usersQuery)
.UniqueResult<int>();
Sorry for the delay in implementing it.
Richard
--------------------------------------------------
From: "Schotime" <adamsc...@gmail.com>
Sent: Saturday, February 13, 2010 10:32 AM
To: "nhusers" <nhu...@googlegroups.com>
Subject: [nhusers] Re: QueryOver issues