我试图实现一个方法,它将创建我需要的所有标准并执行并返回数据。这是方法。
/// <summary>
/// Fetches a paged collection of entities from the database. Use this method when you only need a certain amount of information.
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <param name="pred"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <returns></returns>
public IEnumerable<TType> FetchPagedCollection<TType>(System.Linq.Expressions.Expression<Func<TType, bool>> pred, int pageNumber, int pageSize, out int totalRecords) where TType : class
{
var multiCriteria = this._session.CreateMultiCriteria();
ICriteria dataCriteria = this._session.CreateCriteria<TType>().Add(this.HandleBinaryExpression<TType>(pred));
ICriteria countCriteria = CriteriaTransformer.TransformToRowCount(dataCriteria);//Changed this from Clone since it's what I really want...
dataCriteria.SetMaxResults(pageSize);
dataCriteria.SetFirstResult((pageNumber - 1) * pageSize);
//countCriteria.SetProjection(Projections.RowCount());
multiCriteria.Add<TType>(countCriteria);
multiCriteria.Add<TType>(dataCriteria);
IList result = multiCriteria.List(); <--Throws exception here.
IEnumerable<TType> data = (IEnumerable<TType>)result[0];
IList counts = (IList)result[1];
totalRecords = (int)counts[0];
if (this._ownsTransaction)
this._transaction.Commit();
return data;
}
异常是有道理的,但我不确定如何解决它。该异常表明无法执行计数查询,因为返回值“0”不是类型为FavoriteWorker“
这是实际的异常文本:
{"Error executing multi criteria :
[SELECT count(*) as y0_ FROM FavoriteWorkers this_ WHERE this_.UserID = ?p0;\r\n
SELECT this_.FavoriteWorkerID as Favorite1_0_0_, this_.UserID as UserID0_0_
FROM FavoriteWorkers this_ WHERE this_.UserID = ?p1 limit ?p2;]"}
InnerException Text
{"The value \"0\" is not of type \"FavoriteWorkerEntity\"
and cannot be used in this generic collection.\r\nParameter name: value"}
有人有任何想法吗?
创建的查询类似于此SO答案:https://stackoverflow.com/a/433210/385565