2018-02-11 20:55:29 -05:00
|
|
|
|
using PoweredSoft.DynamicLinq.Fluent;
|
|
|
|
|
using PoweredSoft.DynamicLinq.Helpers;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-03-06 20:59:17 -05:00
|
|
|
|
namespace PoweredSoft.DynamicLinq
|
2018-02-11 20:55:29 -05:00
|
|
|
|
{
|
|
|
|
|
public static class QueryableExtensions
|
|
|
|
|
{
|
2018-03-14 21:08:17 -04:00
|
|
|
|
public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value,
|
|
|
|
|
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
|
2018-03-06 20:56:43 -05:00
|
|
|
|
QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
|
2018-02-11 20:55:29 -05:00
|
|
|
|
{
|
2018-03-06 20:56:43 -05:00
|
|
|
|
query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision));
|
2018-02-11 20:55:29 -05:00
|
|
|
|
return query;
|
|
|
|
|
}
|
2018-03-07 21:16:15 -05:00
|
|
|
|
|
2018-03-14 20:25:47 -04:00
|
|
|
|
public static IQueryable<T> Where<T>(this IQueryable<T> query, Action<WhereBuilder> callback)
|
2018-03-07 21:16:15 -05:00
|
|
|
|
=> query.Query(callback);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
2018-03-14 21:08:17 -04:00
|
|
|
|
public static IQueryable<T> Query<T>(this IQueryable<T> query, Action<WhereBuilder> callback)
|
2018-02-11 20:55:29 -05:00
|
|
|
|
{
|
2018-03-14 20:25:47 -04:00
|
|
|
|
var queryBuilder = new WhereBuilder(query);
|
2018-02-11 20:55:29 -05:00
|
|
|
|
callback(queryBuilder);
|
|
|
|
|
var ret = queryBuilder.Build();
|
2018-03-14 20:25:47 -04:00
|
|
|
|
return (IQueryable<T>)ret;
|
2018-02-11 20:55:29 -05:00
|
|
|
|
}
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
2018-03-14 19:50:43 -04:00
|
|
|
|
// generic.
|
|
|
|
|
public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string path, QueryOrderByDirection direction, bool append)
|
2018-02-12 05:18:44 -05:00
|
|
|
|
{
|
2018-03-14 19:50:43 -04:00
|
|
|
|
IQueryable queryable = query;
|
|
|
|
|
query = queryable.OrderBy(path, direction, append) as IQueryable<T>;
|
|
|
|
|
return query;
|
2018-02-12 05:18:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string path)
|
2018-03-14 19:50:43 -04:00
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Ascending, false);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
|
|
|
|
public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string path)
|
2018-03-14 19:50:43 -04:00
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Descending, false);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
|
|
|
|
public static IQueryable<T> ThenBy<T>(this IQueryable<T> query, string path)
|
2018-03-14 19:50:43 -04:00
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Ascending, true);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
|
|
|
|
public static IQueryable<T> ThenByDescending<T>(this IQueryable<T> query, string path)
|
2018-03-14 19:50:43 -04:00
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Descending, true);
|
|
|
|
|
|
|
|
|
|
// non generic.
|
|
|
|
|
public static IQueryable OrderBy(this IQueryable query, string path, QueryOrderByDirection direction, bool append)
|
2018-02-12 05:18:44 -05:00
|
|
|
|
{
|
2018-03-14 19:50:43 -04:00
|
|
|
|
var qb = new OrderByBuilder(query);
|
|
|
|
|
qb.OrderBy(path, direction, append);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
var ret = qb.Build();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-03-09 00:22:12 -05:00
|
|
|
|
|
2018-03-14 19:50:43 -04:00
|
|
|
|
public static IQueryable OrderBy(this IQueryable query, string path)
|
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Ascending, false);
|
|
|
|
|
|
|
|
|
|
public static IQueryable OrderByDescending(this IQueryable query, string path)
|
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Descending, false);
|
|
|
|
|
|
|
|
|
|
public static IQueryable ThenBy(this IQueryable query, string path)
|
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Ascending, true);
|
|
|
|
|
|
|
|
|
|
public static IQueryable ThenByDescending(this IQueryable query, string path)
|
|
|
|
|
=> query.OrderBy(path, QueryOrderByDirection.Descending, true);
|
|
|
|
|
|
|
|
|
|
// group by
|
2018-03-08 22:59:18 -05:00
|
|
|
|
public static IQueryable GroupBy<T>(this IQueryable<T> query, string path)
|
2018-03-09 00:22:12 -05:00
|
|
|
|
=> QueryableHelpers.GroupBy(query, typeof(T), path);
|
2018-03-08 22:59:18 -05:00
|
|
|
|
|
|
|
|
|
public static IQueryable GroupBy(this IQueryable query, Type type, string path)
|
2018-03-09 00:22:12 -05:00
|
|
|
|
=> QueryableHelpers.GroupBy(query, type, path);
|
|
|
|
|
|
|
|
|
|
public static IQueryable GroupBy<T>(this IQueryable<T> query, Action<GroupBuilder> callback)
|
|
|
|
|
=> query.GroupBy(typeof(T), callback);
|
|
|
|
|
|
|
|
|
|
public static IQueryable GroupBy(this IQueryable query, Type type, Action<GroupBuilder> callback)
|
2018-03-08 22:59:18 -05:00
|
|
|
|
{
|
2018-03-14 20:34:41 -04:00
|
|
|
|
var groupBuilder = new GroupBuilder(query);
|
2018-03-09 00:22:12 -05:00
|
|
|
|
callback(groupBuilder);
|
2018-03-14 20:34:41 -04:00
|
|
|
|
var ret = groupBuilder.Build();
|
|
|
|
|
return ret;
|
2018-03-08 22:59:18 -05:00
|
|
|
|
}
|
2018-03-12 23:26:41 -04:00
|
|
|
|
|
|
|
|
|
public static IQueryable Select(this IQueryable query, Action<SelectBuilder> callback)
|
|
|
|
|
{
|
2018-03-14 20:34:41 -04:00
|
|
|
|
var sb = new SelectBuilder(query);
|
2018-03-12 23:26:41 -04:00
|
|
|
|
callback(sb);
|
2018-03-14 20:34:41 -04:00
|
|
|
|
var ret = sb.Build();
|
|
|
|
|
return ret;
|
2018-03-13 22:01:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<object> ToObjectList(this IQueryable query)
|
|
|
|
|
{
|
2018-03-14 20:34:41 -04:00
|
|
|
|
// Expression call tolist?
|
2018-03-13 22:01:21 -04:00
|
|
|
|
var ret = new List<object>();
|
|
|
|
|
foreach (var o in query)
|
|
|
|
|
ret.Add(o);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<dynamic> ToDynamicList(this IQueryable query)
|
|
|
|
|
{
|
|
|
|
|
var ret = new List<dynamic>();
|
|
|
|
|
foreach (var o in query)
|
|
|
|
|
ret.Add(o);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2018-03-12 23:26:41 -04:00
|
|
|
|
}
|
2018-03-14 21:08:17 -04:00
|
|
|
|
public static List<DynamicClass> ToDynamicClassList(this IQueryable query)
|
|
|
|
|
{
|
|
|
|
|
if (!typeof(DynamicClass).IsAssignableFrom(query.ElementType))
|
|
|
|
|
throw new Exception($"{query.ElementType} does not inherit DynamicClass");
|
|
|
|
|
|
|
|
|
|
var ret = query.Cast<DynamicClass>().ToList();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-02-11 20:55:29 -05:00
|
|
|
|
}
|
|
|
|
|
}
|