46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using Raven.Client.Documents;
|
|
using Raven.Client.Documents.Linq;
|
|
using Raven.Client.Documents.Session;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using zero.Core.Entities;
|
|
|
|
namespace zero.Core.Extensions
|
|
{
|
|
public static class RavenQueryableExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static async Task<ListResult<T>> ToQueriedListAsync<T>(this IRavenQueryable<T> queryable, ListQuery<T> query)
|
|
{
|
|
queryable = queryable.Statistics(out QueryStatistics stats);
|
|
|
|
IQueryable<T> rawQuery = queryable;
|
|
|
|
if (query != null)
|
|
{
|
|
if (!query.Search.IsNullOrEmpty() && query.SearchSelector != null)
|
|
{
|
|
rawQuery = rawQuery.SearchIf(query.SearchSelector, query.Search, "*", "*");
|
|
}
|
|
|
|
if (!query.OrderBy.IsNullOrEmpty())
|
|
{
|
|
rawQuery = rawQuery.OrderBy(query.OrderBy, query.OrderIsDescending, query.OrderType == ListQueryOrderType.String ? OrderingType.String : OrderingType.Double);
|
|
}
|
|
|
|
if (query.PageSize > 0)
|
|
{
|
|
rawQuery = rawQuery.Paging(query.Page, query.PageSize);
|
|
}
|
|
}
|
|
|
|
List<T> items = await rawQuery.ToListAsync();
|
|
|
|
return new ListResult<T>(items, stats.TotalResults, query.Page, query.PageSize);
|
|
}
|
|
}
|
|
}
|