如何将 IQueryOver<TRoot> 方法应用于 IQueryOver?
How to apply IQueryOver<TRoot> methods to IQueryOver?
我正在用接口 IQueryOver 包装 QueryOver。感谢 UnderlyingCriteria 我可以访问条件。
我想使用 IQueryOver 方法,例如 Take、Skip ...
我的意图是要应用一个函数列表,在这种情况下我有一个
public IQueryOver QueryOver { get; set; }
/// <summary>
/// Set paging (before querying!)
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
public void Paging(int pageNumber, int pageSize)
{
if (pageNumber > 0 && pageSize > 0)
{
_pageSize = pageSize;
_pageNumber = pageNumber;
NHibernate.ICriteria paging(IQueryOver str) => str.UnderlyingCriteria.SetFirstResult(pageSize * (pageNumber - 1)).SetMaxResults(pageSize);
_funcs.Add(paging);
}
}
那我想包装List
public IList<T> List()
{
IList<T> result = null;
try
{
BeginTransaction();
try
{
//foreach (var func in _funcs)
//{
// QueryOvern = func.Invoke(QueryOver); // THIS CANT'T WORKS
//}
result = QueryOver.UnderlyingCriteria.
SetResultTransformer(Transformers.AliasToBean<T>()).
List<T>();
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
result = new List<T>();
RollBackTransaction();
}
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
return result;
}
这行得通,我的意思是 List() 行得通,但是哇可以将函数应用于 ICriteria 的 IQueryOver 吗?
提前致谢
真遗憾,每次它应用标准时,UnderlyngCriteria 都会添加它。所以 _criterias 是 Action
的列表
private readonly IList<Action<IQueryOver>> _criterias = null; // [instantiated into constructor]
public void Paging(int pageNumber, int pageSize)
{
if (pageNumber > 0 && pageSize > 0)
{
_pageSize = pageSize;
_pageNumber = pageNumber;
void paging(IQueryOver queryOver) => queryOver.Paging(pageNumber, pageSize);
_criterias.Add(paging);
}
}
即可申请
public IList<T> List()
{
IList<T> result = null;
try
{
BeginTransaction();
try
{
foreach (var criteria in _criterias)
{
criteria.Invoke(QueryOver);
}
result = QueryOver.UnderlyingCriteria.
SetResultTransformer(Transformers.AliasToBean<T>()).
List<T>();
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
result = new List<T>();
RollBackTransaction();
}
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
return result;
}
我做了分页的扩展方法
public static class CustomExtensions
{
public static NHibernate.ICriteria Paging(this IQueryOver queryOver, int pageNumber, int pageSize)
{
return queryOver.UnderlyingCriteria.SetFirstResult(pageSize * (pageNumber - 1)).SetMaxResults(pageSize);
}
}
我正在用接口 IQueryOver 包装 QueryOver。感谢 UnderlyingCriteria 我可以访问条件。
我想使用 IQueryOver 方法,例如 Take、Skip ...
我的意图是要应用一个函数列表,在这种情况下我有一个
public IQueryOver QueryOver { get; set; }
/// <summary>
/// Set paging (before querying!)
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
public void Paging(int pageNumber, int pageSize)
{
if (pageNumber > 0 && pageSize > 0)
{
_pageSize = pageSize;
_pageNumber = pageNumber;
NHibernate.ICriteria paging(IQueryOver str) => str.UnderlyingCriteria.SetFirstResult(pageSize * (pageNumber - 1)).SetMaxResults(pageSize);
_funcs.Add(paging);
}
}
那我想包装List
public IList<T> List()
{
IList<T> result = null;
try
{
BeginTransaction();
try
{
//foreach (var func in _funcs)
//{
// QueryOvern = func.Invoke(QueryOver); // THIS CANT'T WORKS
//}
result = QueryOver.UnderlyingCriteria.
SetResultTransformer(Transformers.AliasToBean<T>()).
List<T>();
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
result = new List<T>();
RollBackTransaction();
}
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
return result;
}
这行得通,我的意思是 List() 行得通,但是哇可以将函数应用于 ICriteria 的 IQueryOver 吗?
提前致谢
真遗憾,每次它应用标准时,UnderlyngCriteria 都会添加它。所以 _criterias 是 Action
的列表private readonly IList<Action<IQueryOver>> _criterias = null; // [instantiated into constructor]
public void Paging(int pageNumber, int pageSize)
{
if (pageNumber > 0 && pageSize > 0)
{
_pageSize = pageSize;
_pageNumber = pageNumber;
void paging(IQueryOver queryOver) => queryOver.Paging(pageNumber, pageSize);
_criterias.Add(paging);
}
}
即可申请
public IList<T> List()
{
IList<T> result = null;
try
{
BeginTransaction();
try
{
foreach (var criteria in _criterias)
{
criteria.Invoke(QueryOver);
}
result = QueryOver.UnderlyingCriteria.
SetResultTransformer(Transformers.AliasToBean<T>()).
List<T>();
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
result = new List<T>();
RollBackTransaction();
}
}
catch (Exception ex)
{
log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
return result;
}
我做了分页的扩展方法
public static class CustomExtensions
{
public static NHibernate.ICriteria Paging(this IQueryOver queryOver, int pageNumber, int pageSize)
{
return queryOver.UnderlyingCriteria.SetFirstResult(pageSize * (pageNumber - 1)).SetMaxResults(pageSize);
}
}