1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
using (ISession session = NHibernateSessionProvider.GetSession())
{
// Good thing, count and results are retrieve in the same transaction
using (ITransaction transaction = session.BeginTransaction())
{
// FutureValue is used to execute two requests in one database call (or round-trip)
IFutureValue<int> count = session.CreateCriteria(typeof(TUser))
.SetProjection(Projections.Count(Projections.Id()))
.FutureValue<int>();
ICriteria userItems = session.CreateCriteria(typeof(TUser));
if (userId != "none")
{
userItems.Add(Restrictions.Eq("UserLogin", userId));
}
if (userEmail != "none")
{
userItems.Add(Restrictions.Eq("UserEmail", userEmail));
}
if (groupName != "none")
{
userItems.Add(Restrictions.Eq("TGroup", userGroup));
}
userItems.CreateAlias("TGroup", "TGroup", JoinType.LeftOuterJoin); // Load group info, even if there's no group attached, first parameter correspond to the table name, second parameter is the alias (same value for simplicity)
userItems.Future<TUser>();
recordsCount = count.Value;
result = userItems.List<TUser>();
// End transaction
transaction.Commit();
}
} |
Partager