anonymous type creation linq.

This commit is contained in:
David Lebée 2018-03-11 00:58:32 -06:00
parent 7278dea56d
commit 515d0bee37
2 changed files with 11 additions and 1 deletions

View File

@ -77,7 +77,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
return ret; return ret;
} }
internal static IQueryable GroupBy(IQueryable query, Type type, List<(string path, string propertyName)> parts) public static IQueryable GroupBy(IQueryable query, Type type, List<(string path, string propertyName)> parts)
{ {
// EXPRESSION // EXPRESSION
var parameter = Expression.Parameter(type, "t"); var parameter = Expression.Parameter(type, "t");
@ -95,6 +95,10 @@ namespace PoweredSoft.DynamicLinq.Helpers
var anonymousType = TypeHelpers.CreateSimpleAnonymousType(fields); var anonymousType = TypeHelpers.CreateSimpleAnonymousType(fields);
var constructorTypes = fields.Select(t => t.type).ToArray();
var constructor = anonymousType.GetConstructor(constructorTypes);
var newExpression = Expression.New(constructor, partExpressions);
var genericMethod = Constants.GroupByMethod.MakeGenericMethod(type, anonymousType);
return query; return query;
} }

View File

@ -35,6 +35,12 @@ namespace PoweredSoft.DynamicLinq.Helpers
return ret; return ret;
} }
/// <summary>
/// Use this to create anonymous type concstructor for LINQ queries :)
/// https://stackoverflow.com/questions/6879279/using-typebuilder-to-create-a-pass-through-constructor-for-the-base-class
/// </summary>
/// <param name="fields"></param>
/// <returns></returns>
internal static TypeInfo CreateSimpleAnonymousType(List<(Type type, string name)> fields) internal static TypeInfo CreateSimpleAnonymousType(List<(Type type, string name)> fields)
{ {
// DYNAMIC TYPE CREATION // DYNAMIC TYPE CREATION