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,7 +14,26 @@ namespace PoweredSoft.DynamicLinq.Test
public void WantedSyntax()
{
var regularSyntax = TestData.Sales
.GroupBy(t => t.ClientId)
.GroupBy(t => t.ClientId);
var dynamicSyntax = TestData.Sales
.AsQueryable()
.GroupBy("ClientId");
/*
var regularSyntax2 = TestData.Sales
.GroupBy(t => new
{
t.ClientId,
t.NetSales
});
var dynamicSyntax2 = TestData.Sales
.AsQueryable()
.GroupBy("ClientId", "NetSales");*/
/*
.Select(t => new
{
TheClientId = t.Key,
@ -24,10 +43,11 @@ namespace PoweredSoft.DynamicLinq.Test
NetSales = t.Sum(t2 => t2.NetSales),
TaxAverage = t.Average(t2 => t2.Tax),
Sales = t.ToList()
});
});*/
/*
var dynamicSyntax = TestData.Sales
.GroupBy("ClientId")
.Select(t =>
{
t.PropertyFromKey("TheClientId", "ClientId");

View File

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

View File

@ -43,6 +43,7 @@ namespace PoweredSoft.DynamicLinq
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 ContainsMethod = typeof(string).GetMethod("Contains");
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();
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.Text;
using System.Threading.Tasks;
using System.Reflection.Emit;
namespace PoweredSoft.DynamicLinq.Helpers
{
@ -76,6 +77,42 @@ namespace PoweredSoft.DynamicLinq.Helpers
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>
/// Returns the right expression for a path supplied.
/// </summary>