QueryOver issues

369 views
Skip to first unread message

Schotime

unread,
Feb 12, 2010, 4:45:13 PM2/12/10
to nhusers
Was trying out the new QueryOver since Linq doesn't have fetch yet,
but i received a different response than expected.

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

Richard Brown (gmail)

unread,
Feb 13, 2010, 5:11:57 AM2/13/10
to nhusers
Hi 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.
>
>

Schotime

unread,
Feb 13, 2010, 5:32:34 AM2/13/10
to nhusers
That makes sense.
This would be a pretty common scenario i would think.

Thanks for the reply.

Adam

On Feb 13, 9:11 pm, "Richard Brown \(gmail\)"

> From: "Schotime" <adamschro...@gmail.com>

Richard Brown (gmail)

unread,
Mar 6, 2010, 6:01:22 PM3/6/10
to nhusers
Hi Adam,

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

Fabio Maulo

unread,
Mar 6, 2010, 10:26:12 PM3/6/10
to nhu...@googlegroups.com
Richard,
why not a IQueryOver method or an extension method ?
var usersQuery =

  mySession.QueryOver<User>()
      ...
      .OrderBy(x => x.LastName).Asc
      .Skip(page * pageSize)
      .Take(pageSize);

var count = usersQuery.Count();
or
var count = usersQuery.RowCount();

2010/3/6 Richard Brown (gmail) <fluk...@googlemail.com>



--
Fabio Maulo

Richard Brown (gmail)

unread,
Mar 7, 2010, 5:03:04 AM3/7/10
to nhu...@googlegroups.com
Of course.  Done.
 
 
IQueryOver<User> userQuery =
  mySession.QueryOver<User>()
    ...
    .OrderBy(u => u.LastName).Asc
    .Skip(page * pageSize)
    .Take(pageSize);
 
IList<User> users = userQuery.List();
int usersCount = userQuery.RowCount();
 
 
(And I should have done that the first time too!)

Fabio Maulo

unread,
Mar 7, 2010, 7:53:54 AM3/7/10
to nhu...@googlegroups.com
ROTOLF!!!!!

2010/3/7 Richard Brown (gmail) <fluk...@googlemail.com>



--
Fabio Maulo

Reply all
Reply to author
Forward
0 new messages