cleaner like this. everyone respects the builder concept with a build method.

This commit is contained in:
David Lebée
2018-03-14 19:34:41 -05:00
parent 18a3756f51
commit 9ab186b811
3 changed files with 41 additions and 13 deletions
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Linq;
using PoweredSoft.DynamicLinq.Helpers;
namespace PoweredSoft.DynamicLinq.Fluent
{
@@ -12,6 +13,13 @@ namespace PoweredSoft.DynamicLinq.Fluent
public bool Empty => !Parts.Any();
public Type EqualityComparerType { get; set; }
public IQueryable Query { get; protected set; }
public GroupBuilder(IQueryable query)
{
Query = query;
}
public GroupBuilder Path(string path, string propertyName = null)
{
if (propertyName == null)
@@ -44,5 +52,14 @@ namespace PoweredSoft.DynamicLinq.Fluent
EqualityComparerType = type;
return this;
}
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;
}
}
}
@@ -1,4 +1,5 @@
using System;
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -17,6 +18,12 @@ namespace PoweredSoft.DynamicLinq.Fluent
public List<SelectPart> Parts = new List<SelectPart>();
public Type DestinationType { get; set; }
public bool Empty => Parts?.Count == 0;
public IQueryable Query { get; protected set; }
public SelectBuilder(IQueryable query)
{
Query = query;
}
protected void throwIfUsedOrEmpty(string propertyName)
{
@@ -108,5 +115,14 @@ namespace PoweredSoft.DynamicLinq.Fluent
});
return this;
}
public virtual IQueryable Build()
{
if (Empty)
throw new Exception("No select specified, please specify at least one select path");
var partsTuple = Parts.Select(t => (selectType: t.SelectType, propertyName: t.PropertyName, path: t.Path)).ToList();
return QueryableHelpers.Select(Query, partsTuple, DestinationType);
}
}
}