2018-03-14 19:12:22 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Helpers;
|
|
|
|
|
using System;
|
2018-03-14 19:50:43 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-03-14 19:12:22 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.DynamicLinq.Fluent
|
|
|
|
|
{
|
2018-03-14 20:36:26 -04:00
|
|
|
|
public class OrderByBuilder : IQueryBuilder
|
2018-03-14 19:12:22 -04:00
|
|
|
|
{
|
2018-03-14 19:50:43 -04:00
|
|
|
|
public IQueryable Query { get; }
|
2018-03-14 19:12:22 -04:00
|
|
|
|
|
2018-03-14 19:50:43 -04:00
|
|
|
|
public OrderByBuilder(IQueryable query)
|
2018-03-14 19:12:22 -04:00
|
|
|
|
{
|
|
|
|
|
Query = query;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 19:50:43 -04:00
|
|
|
|
public virtual IQueryable Build()
|
2018-03-14 19:12:22 -04:00
|
|
|
|
{
|
|
|
|
|
var query = Query;
|
|
|
|
|
|
|
|
|
|
Sorts.ForEach(sort =>
|
|
|
|
|
{
|
2018-03-14 19:50:43 -04:00
|
|
|
|
query = QueryableHelpers.CreateOrderByExpression(query, sort.Path, sort.Direction, sort.Append);
|
2018-03-14 19:12:22 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
}
|
2018-03-14 19:50:43 -04:00
|
|
|
|
|
|
|
|
|
public List<OrderByPart> Sorts { get; protected set; } = new List<OrderByPart>();
|
|
|
|
|
|
|
|
|
|
public virtual OrderByBuilder OrderBy(string path, QueryOrderByDirection direction, bool append)
|
|
|
|
|
{
|
|
|
|
|
if (append == false)
|
|
|
|
|
Sorts.Clear();
|
|
|
|
|
|
|
|
|
|
Sorts.Add(new OrderByPart
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
Direction = direction,
|
|
|
|
|
Append = append
|
|
|
|
|
});
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region shortcuts
|
|
|
|
|
public virtual OrderByBuilder OrderBy(string path)
|
|
|
|
|
=> OrderBy(path, QueryOrderByDirection.Ascending, false);
|
|
|
|
|
|
|
|
|
|
public virtual OrderByBuilder OrderByDescending(string path)
|
|
|
|
|
=> OrderBy(path, QueryOrderByDirection.Descending, false);
|
|
|
|
|
|
|
|
|
|
public virtual OrderByBuilder ThenBy(string path)
|
|
|
|
|
=> OrderBy(path, QueryOrderByDirection.Ascending, true);
|
|
|
|
|
|
|
|
|
|
public virtual OrderByBuilder ThenByDescending(string path)
|
|
|
|
|
=> OrderBy(path, QueryOrderByDirection.Descending, true);
|
|
|
|
|
#endregion
|
2018-03-14 19:12:22 -04:00
|
|
|
|
}
|
|
|
|
|
}
|