order by is refactored to not force generics anymore.

This commit is contained in:
David Lebée
2018-03-14 18:50:43 -05:00
parent d6265c3b8d
commit c4a398a8cb
8 changed files with 75 additions and 109 deletions
@@ -1,30 +1,60 @@
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PoweredSoft.DynamicLinq.Fluent
{
public class OrderByBuilder<T> : OrderByBuilderBase
public class OrderByBuilder
{
public IQueryable<T> Query { get; }
public IQueryable Query { get; }
public OrderByBuilder(IQueryable<T> query)
public OrderByBuilder(IQueryable query)
{
Query = query;
}
public virtual IQueryable<T> Build()
public virtual IQueryable Build()
{
var query = Query;
Sorts.ForEach(sort =>
{
query = QueryableHelpers.CreateOrderByExpression(query, sort.Path, sort.sortDirection, sort.AppendSort);
query = QueryableHelpers.CreateOrderByExpression(query, sort.Path, sort.Direction, sort.Append);
});
return query;
}
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
}
}
@@ -1,66 +0,0 @@
using System.Collections.Generic;
namespace PoweredSoft.DynamicLinq.Fluent
{
public class OrderByBuilderBase
{
public List<OrderByPart> Sorts { get; protected set; } = new List<OrderByPart>();
public virtual OrderByBuilderBase Sort(string path, QuerySortDirection sortDirection, bool appendSort)
{
Sorts.Add(new OrderByPart
{
Path = path,
sortDirection = sortDirection,
AppendSort = appendSort
});
return this;
}
public virtual OrderByBuilderBase OrderBy(string path)
{
Sorts.Clear();
Sorts.Add(new OrderByPart
{
Path = path,
sortDirection = QuerySortDirection.Ascending,
AppendSort = false
});
return this;
}
public virtual OrderByBuilderBase OrderByDescending(string path)
{
Sorts.Clear();
Sorts.Add(new OrderByPart
{
Path = path,
sortDirection = QuerySortDirection.Descending,
AppendSort = false
});
return this;
}
public virtual OrderByBuilderBase ThenBy(string path)
{
Sorts.Add(new OrderByPart
{
Path = path,
sortDirection = QuerySortDirection.Ascending,
AppendSort = true
});
return this;
}
public virtual OrderByBuilderBase ThenByDescending(string path)
{
Sorts.Add(new OrderByPart
{
Path = path,
sortDirection = QuerySortDirection.Descending,
AppendSort = true
});
return this;
}
}
}
@@ -10,8 +10,8 @@ namespace PoweredSoft.DynamicLinq.Fluent
{
public string Path { get; set; }
public QuerySortDirection sortDirection { get; set; } = QuerySortDirection.Ascending;
public QueryOrderByDirection Direction { get; set; } = QueryOrderByDirection.Ascending;
public bool AppendSort { get; set; }
public bool Append { get; set; }
}
}