目前分页支持单表
public IList<TEntity> GetPaged<TEntity>(out int total, Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, int index = , int size = ) where TEntity : class
{
int skipCount = (index - ) * size;
var _reset = Get(filter, orderBy);
total = _reset.Count();
_reset = skipCount > ? _reset.Skip(skipCount).Take(size) : _reset.Take(size);
return _reset.ToList();
} public IQueryable<TEntity> Get<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null) where TEntity : class
{
IQueryable<TEntity> query = dbContext.Set<TEntity>();
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
return orderBy(query).AsQueryable();
}
else
{
return query.AsQueryable();
}
}
使用
int count;
var a = respository.GetPaged<S_Users>(out count, m => m.LoginName.Contains("a"), q => q.OrderBy(m => m.LastDate),, );
是不是很方便?
如何多个排序条件?
var a = respository.GetPaged<S_Users>(out count, m => m.LoginName.Contains("a"), q => q.OrderBy(m => m.LastDate).ThenByDescending(m=>m.LoginNum),, );
最后跟踪看一下映射出来的SQL是什么样的?
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT([Limit1].[C1]) AS [A1]
FROM ( SELECT TOP () [Project1].[C1] AS [C1]
FROM ( SELECT
AS [C1],
[Extent1].[LastDate] AS [LastDate]
FROM [dbo].[S_Users] AS [Extent1]
WHERE [Extent1].[LoginName] LIKE N'%a%'
) AS [Project1]
ORDER BY [Project1].[LastDate] ASC
) AS [Limit1]
) AS [GroupBy1] SELECT TOP ()
[Extent1].[UserID] AS [UserID],
[Extent1].[LoginName] AS [LoginName],
[Extent1].[Password] AS [Password],
[Extent1].[UserName] AS [UserName],
[Extent1].[UserType] AS [UserType],
[Extent1].[Tel] AS [Tel],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[LoginNum] AS [LoginNum],
[Extent1].[LastDate] AS [LastDate],
[Extent1].[ShopID] AS [ShopID]
FROM [dbo].[S_Users] AS [Extent1]
WHERE [Extent1].[LoginName] LIKE N'%a%'
ORDER BY [Extent1].[LoginName] ASC, [Extent1].[LoginNum] DESC