simple grouping.

This commit is contained in:
David Lebée 2018-03-08 21:59:18 -06:00
parent 17dcbb492f
commit 64d60b6942
5 changed files with 95 additions and 23 deletions

View File

@ -14,31 +14,51 @@ namespace PoweredSoft.DynamicLinq.Test
public void WantedSyntax() public void WantedSyntax()
{ {
var regularSyntax = TestData.Sales var regularSyntax = TestData.Sales
.GroupBy(t => t.ClientId) .GroupBy(t => t.ClientId);
.Select(t => new
{
TheClientId = t.Key,
Count = t.Count(),
CountClientId = t.Count(t2 => t2.ClientId > 1),
LongCount = t.LongCount(),
NetSales = t.Sum(t2 => t2.NetSales),
TaxAverage = t.Average(t2 => t2.Tax),
Sales = t.ToList()
});
/*
var dynamicSyntax = TestData.Sales var dynamicSyntax = TestData.Sales
.GroupBy("ClientId") .AsQueryable()
.Select(t => .GroupBy("ClientId");
/*
var regularSyntax2 = TestData.Sales
.GroupBy(t => new
{ {
t.PropertyFromKey("TheClientId", "ClientId"); t.ClientId,
t.Count("Count"); t.NetSales
// don't have to implement right away. });
t.Count("CountClientId", "ClientId", ConditionOperators.GreaterThan, 1);
t.LongCount("LongCount"); var dynamicSyntax2 = TestData.Sales
t.Sum("NetSales"); .AsQueryable()
t.Average("TaxAverage", "Tax"); .GroupBy("ClientId", "NetSales");*/
t.ToList("Sales");
});*/ /*
.Select(t => new
{
TheClientId = t.Key,
Count = t.Count(),
CountClientId = t.Count(t2 => t2.ClientId > 1),
LongCount = t.LongCount(),
NetSales = t.Sum(t2 => t2.NetSales),
TaxAverage = t.Average(t2 => t2.Tax),
Sales = t.ToList()
});*/
/*
.Select(t =>
{
t.PropertyFromKey("TheClientId", "ClientId");
t.Count("Count");
// don't have to implement right away.
t.Count("CountClientId", "ClientId", ConditionOperators.GreaterThan, 1);
t.LongCount("LongCount");
t.Sum("NetSales");
t.Average("TaxAverage", "Tax");
t.ToList("Sales");
});*/
} }
} }
} }

View File

@ -62,6 +62,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="ComplexQueriesTests.cs" /> <Compile Include="ComplexQueriesTests.cs" />
<Compile Include="ConstantTests.cs" /> <Compile Include="ConstantTests.cs" />
<Compile Include="GroupingTests.cs" />
<Compile Include="Helpers\QueryableAssert.cs" /> <Compile Include="Helpers\QueryableAssert.cs" />
<Compile Include="InTests.cs" /> <Compile Include="InTests.cs" />
<Compile Include="ShortcutTests.cs" /> <Compile Include="ShortcutTests.cs" />

View File

@ -43,6 +43,7 @@ namespace PoweredSoft.DynamicLinq
internal static class Constants internal static class Constants
{ {
internal static readonly MethodInfo GroupByMethod = typeof(Queryable).GetMethods().First(t => t.Name == "GroupBy");
internal static readonly MethodInfo StringEqualWithComparisation = typeof(string).GetMethod("Equals", new Type[] { typeof(string), typeof(StringComparison) }); internal static readonly MethodInfo StringEqualWithComparisation = typeof(string).GetMethod("Equals", new Type[] { typeof(string), typeof(StringComparison) });
internal static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains"); internal static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains");
internal static readonly MethodInfo StartsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }); internal static readonly MethodInfo StartsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });

View File

@ -69,5 +69,18 @@ namespace PoweredSoft.DynamicLinq
var ret = qb.Build(); var ret = qb.Build();
return ret; return ret;
} }
public static IQueryable GroupBy<T>(this IQueryable<T> query, string path)
where T : class
{
var ret = query.GroupBy(typeof(T), path);
return ret as IQueryable;
}
public static IQueryable GroupBy(this IQueryable query, Type type, string path)
{
var ret = QueryableHelpers.GroupBy(query, type, path);
return ret;
}
} }
} }

View File

@ -6,6 +6,7 @@ using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Reflection.Emit;
namespace PoweredSoft.DynamicLinq.Helpers namespace PoweredSoft.DynamicLinq.Helpers
{ {
@ -76,6 +77,42 @@ namespace PoweredSoft.DynamicLinq.Helpers
return ret; return ret;
} }
public static IQueryable GroupBy(IQueryable query, Type type, string path)
{
var parameter = Expression.Parameter(type, "t");
var field = QueryableHelpers.ResolvePathForExpression(parameter, path);
var lambda = Expression.Lambda(field, parameter);
var genericMethod = Constants.GroupByMethod.MakeGenericMethod(type, field.Type);
var groupByEpression = Expression.Call(genericMethod, query.Expression, lambda);
var result = query.Provider.CreateQuery(groupByEpression);
return result;
}
private static IQueryable GroupByAnonymousObject(IQueryable query, Type type, ParameterExpression parameter, List<Expression> fields)
{
throw new NotSupportedException();
/*
var dynamicAssemblyName = new AssemblyName("PoweredSoft.DynamicLinq.DynamicTypes");
var dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(dynamicAssemblyName, System.Reflection.Emit.AssemblyBuilderAccess.Run);
System.Reflection.Emit.ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("TempAssembly");
TypeBuilder dynamicAnonymousType = dynamicModule.DefineType("AnonymousType", TypeAttributes.Public);
int i = 0;
fields.ForEach(field =>
{
var fieldName = $"A_{++i}"; // tODO
dynamicAnonymousType.DefineField(fieldName.
});
dynamicAnonymousType.DefineField(, typeof(TFieldA), FieldAttributes.Public);
dynamicAnonymousType.DefineField(fieldNameB, typeof(TFieldB), FieldAttributes.Public);
return dynamicAnonymousType.CreateType();
throw new NotImplementedException(); */
}
/// <summary> /// <summary>
/// Returns the right expression for a path supplied. /// Returns the right expression for a path supplied.
/// </summary> /// </summary>