Skip to content

Instantly share code, notes, and snippets.

@Tazer
Created February 27, 2012 12:12
Show Gist options
  • Save Tazer/1923345 to your computer and use it in GitHub Desktop.
Save Tazer/1923345 to your computer and use it in GitHub Desktop.
public class TheDefaultCategory : ICreateCriteria<Category>
{
public DetachedCriteria GetCriteria()
{
return DetachedCriteria.For<Category>().Add(Restrictions.Eq("Default", true));
}
}
public IEnumerable<T> Matching<T>(ICreateCriteria<T> query, params IAppendCriterion[] extraCriterias)
{
var criteria = query.GetCriteria();
foreach (var criterion in extraCriterias)
{
criterion.Append(criteria);
}
return criteria.GetExecutableCriteria(_session).List<T>();
}
public IPagedList<T> Matching<T>(ICreateCriteria<T> query, IPagedQuerySettings paging, params IAppendCriterion[] extraCriterias)
{
var multiCriteria = _session.CreateMultiCriteria();
var detachedCriteria = query.GetCriteria();
foreach (var criterion in extraCriterias)
{
criterion.Append(detachedCriteria);
}
var totalCountCriteria = CriteriaTransformer.Clone((DetachedCriteria) detachedCriteria);
//Ta bort alla ORDER BY
totalCountCriteria.ClearOrders();
totalCountCriteria.SetProjection(Projections.RowCount());
//Get the rows
multiCriteria.Add(totalCountCriteria);
if (paging != null)
{
detachedCriteria.SetFirstResult(paging.GetFirstResult()).SetMaxResults(paging.PageSize);
}
//Get the actual result
multiCriteria.Add((ICriteria) detachedCriteria.GetExecutableCriteria(_session));
var results = multiCriteria.List();
int count = (int)((IList)results[0])[0];
var entities = (IList)results[1];
return new PagedList<T>(paging.Page, paging.PageSize, count, entities.Cast<T>());
}
public interface ICreateCriteria<T> : ICreateCriteria
{ }
public interface ICreateCriteria
{
DetachedCriteria GetCriteria();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment