2018-03-09 00:22:12 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Linq;
|
2018-03-14 20:34:41 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Helpers;
|
2018-03-09 00:22:12 -05:00
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.DynamicLinq.Fluent
|
|
|
|
|
{
|
2018-03-14 20:36:26 -04:00
|
|
|
|
public class GroupBuilder : IQueryBuilder
|
2018-03-09 00:22:12 -05:00
|
|
|
|
{
|
|
|
|
|
public List<(string path, string propertyName)> Parts { get; set; } = new List<(string path, string propertyName)>();
|
2018-03-12 18:23:02 -04:00
|
|
|
|
public Type Type { get; set; }
|
2018-03-09 00:22:12 -05:00
|
|
|
|
public bool Empty => !Parts.Any();
|
2018-03-12 19:00:02 -04:00
|
|
|
|
public Type EqualityComparerType { get; set; }
|
2018-03-09 00:22:12 -05:00
|
|
|
|
|
2018-03-14 20:34:41 -04:00
|
|
|
|
public IQueryable Query { get; protected set; }
|
|
|
|
|
|
|
|
|
|
public GroupBuilder(IQueryable query)
|
|
|
|
|
{
|
|
|
|
|
Query = query;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 00:22:12 -05:00
|
|
|
|
public GroupBuilder Path(string path, string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
if (propertyName == null)
|
|
|
|
|
{
|
|
|
|
|
var name = path;
|
|
|
|
|
if (name.Contains("."))
|
|
|
|
|
{
|
|
|
|
|
var parts = name.Split('.');
|
|
|
|
|
name = parts[parts.Length - 1]; // the last one.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Parts.Any(t => t.propertyName == name))
|
|
|
|
|
throw new Exception($"{name} is already taken by another group part, you can specify a property name instead to resolve this issue");
|
|
|
|
|
|
|
|
|
|
propertyName = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Parts.Add((path, propertyName));
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2018-03-12 18:23:02 -04:00
|
|
|
|
|
|
|
|
|
public GroupBuilder UseType(Type type)
|
|
|
|
|
{
|
|
|
|
|
Type = type;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2018-03-12 19:00:02 -04:00
|
|
|
|
|
|
|
|
|
public GroupBuilder EqualityComparer(Type type)
|
|
|
|
|
{
|
|
|
|
|
EqualityComparerType = type;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2018-03-14 20:34:41 -04:00
|
|
|
|
|
|
|
|
|
public virtual IQueryable Build()
|
|
|
|
|
{
|
|
|
|
|
if (Empty)
|
|
|
|
|
throw new Exception("No group specified, please specify at least one group");
|
|
|
|
|
|
|
|
|
|
var ret = QueryableHelpers.GroupBy(Query, Query.ElementType, Parts, Type, EqualityComparerType);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-03-09 00:22:12 -05:00
|
|
|
|
}
|
|
|
|
|
}
|