From 9ab186b811c9ddd393e22316a1b3436dcf7a5efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Leb=C3=A9e?= Date: Wed, 14 Mar 2018 19:34:41 -0500 Subject: [PATCH] cleaner like this. everyone respects the builder concept with a build method. --- .../Extensions/QueryableExtensions.cs | 19 +++++++------------ .../Fluent/Group/GroupBuilder.cs | 17 +++++++++++++++++ .../Fluent/Select/SelectBuilder.cs | 18 +++++++++++++++++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs b/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs index 18a9336..bd374fe 100644 --- a/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs +++ b/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs @@ -83,28 +83,23 @@ namespace PoweredSoft.DynamicLinq public static IQueryable GroupBy(this IQueryable query, Type type, Action callback) { - var groupBuilder = new GroupBuilder(); + var groupBuilder = new GroupBuilder(query); callback(groupBuilder); - if (groupBuilder.Empty) - throw new Exception("No group specified, please specify at least one group"); - - return QueryableHelpers.GroupBy(query, type, groupBuilder.Parts, groupBuilder.Type, groupBuilder.EqualityComparerType); + var ret = groupBuilder.Build(); + return ret; } public static IQueryable Select(this IQueryable query, Action callback) { - var sb = new SelectBuilder(); + var sb = new SelectBuilder(query); callback(sb); - if (sb.Empty) - throw new Exception("No select specified, please specify at least one select path"); - - return QueryableHelpers.Select(query, - sb.Parts.Select(t => (selectType: t.SelectType, propertyName: t.PropertyName, path: t.Path)).ToList(), - sb.DestinationType); + var ret = sb.Build(); + return ret; } public static List ToObjectList(this IQueryable query) { + // Expression call tolist? var ret = new List(); foreach (var o in query) ret.Add(o); diff --git a/PoweredSoft.DynamicLinq/Fluent/Group/GroupBuilder.cs b/PoweredSoft.DynamicLinq/Fluent/Group/GroupBuilder.cs index 88f9ebc..353e9eb 100644 --- a/PoweredSoft.DynamicLinq/Fluent/Group/GroupBuilder.cs +++ b/PoweredSoft.DynamicLinq/Fluent/Group/GroupBuilder.cs @@ -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; + } } } diff --git a/PoweredSoft.DynamicLinq/Fluent/Select/SelectBuilder.cs b/PoweredSoft.DynamicLinq/Fluent/Select/SelectBuilder.cs index 6379769..50fe5c3 100644 --- a/PoweredSoft.DynamicLinq/Fluent/Select/SelectBuilder.cs +++ b/PoweredSoft.DynamicLinq/Fluent/Select/SelectBuilder.cs @@ -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 Parts = new List(); 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); + } } }