simple grouping.
This commit is contained in:
parent
17dcbb492f
commit
64d60b6942
@ -14,7 +14,26 @@ 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);
|
||||||
|
|
||||||
|
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
|
.Select(t => new
|
||||||
{
|
{
|
||||||
TheClientId = t.Key,
|
TheClientId = t.Key,
|
||||||
@ -24,10 +43,11 @@ namespace PoweredSoft.DynamicLinq.Test
|
|||||||
NetSales = t.Sum(t2 => t2.NetSales),
|
NetSales = t.Sum(t2 => t2.NetSales),
|
||||||
TaxAverage = t.Average(t2 => t2.Tax),
|
TaxAverage = t.Average(t2 => t2.Tax),
|
||||||
Sales = t.ToList()
|
Sales = t.ToList()
|
||||||
});
|
});*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
var dynamicSyntax = TestData.Sales
|
|
||||||
.GroupBy("ClientId")
|
|
||||||
.Select(t =>
|
.Select(t =>
|
||||||
{
|
{
|
||||||
t.PropertyFromKey("TheClientId", "ClientId");
|
t.PropertyFromKey("TheClientId", "ClientId");
|
||||||
|
@ -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" />
|
||||||
|
@ -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) });
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user