Allow possibility of specifying the type to group with :)

This commit is contained in:
David Lebée 2018-03-12 17:23:02 -05:00
parent 9db5d74284
commit 165cc15d0a
5 changed files with 28 additions and 6 deletions

View File

@ -8,6 +8,12 @@ using PoweredSoft.DynamicLinq;
namespace PoweredSoft.DynamicLinq.Test
{
internal class TestStructure
{
public long ClientId { get; set; }
public decimal B { get; set; }
}
[TestClass]
public class GroupingTests
{
@ -34,6 +40,11 @@ namespace PoweredSoft.DynamicLinq.Test
.AsQueryable()
.GroupBy(t => t.Path("ClientId").Path("NetSales", "B"));
var dynamicSyntax3 = TestData.Sales
.AsQueryable()
.GroupBy(t => t.UseType(typeof(TestStructure)).Path("ClientId").Path("NetSales", "B"));
/*
.Select(t => new
{

View File

@ -86,7 +86,7 @@ namespace PoweredSoft.DynamicLinq
if (groupBuilder.Empty)
throw new Exception("No group specified, please specify at least one group");
return QueryableHelpers.GroupBy(query, type, groupBuilder.Parts);
return QueryableHelpers.GroupBy(query, type, groupBuilder.Parts, groupBuilder.Type);
}
}
}

View File

@ -8,6 +8,7 @@ namespace PoweredSoft.DynamicLinq.Fluent
public class GroupBuilder
{
public List<(string path, string propertyName)> Parts { get; set; } = new List<(string path, string propertyName)>();
public Type Type { get; set; }
public bool Empty => !Parts.Any();
public GroupBuilder Path(string path, string propertyName = null)
@ -30,5 +31,11 @@ namespace PoweredSoft.DynamicLinq.Fluent
Parts.Add((path, propertyName));
return this;
}
public GroupBuilder UseType(Type type)
{
Type = type;
return this;
}
}
}

View File

@ -77,7 +77,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
return ret;
}
public 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, Type groupToType = null)
{
// EXPRESSION
var parameter = Expression.Parameter(type, "t");
@ -93,7 +93,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
partExpressions.Add((partExpression, part.propertyName));
});
var anonymousType = TypeHelpers.CreateSimpleAnonymousType(fields);
var anonymousType = groupToType ?? TypeHelpers.CreateSimpleAnonymousType(fields);
/*

View File

@ -36,8 +36,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
}
/// <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
/// Use this to create anonymous type
/// </summary>
/// <param name="fields"></param>
/// <returns></returns>
@ -50,11 +49,16 @@ namespace PoweredSoft.DynamicLinq.Helpers
{
CreatePropertyOnType(dynamicType, field.name, field.type);
});
// not needed at the end.
// CreateConstructorWithAllPropsOnType(dynamicType, fields);
var ret = dynamicType.CreateTypeInfo();
return ret;
}
/*
* concstructor
* https://stackoverflow.com/questions/6879279/using-typebuilder-to-create-a-pass-through-constructor-for-the-base-class
* works but wasn't needed at the end.
private static void CreateConstructorWithAllPropsOnType(TypeBuilder dynamicType, List<(Type type, string name)> fields)
{
var ctor = dynamicType.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, fields.Select(t => t.type).ToArray());
@ -76,7 +80,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
}
emitter.Emit(OpCodes.Call, ctor);
emitter.Emit(OpCodes.Ret);
}
}*/
internal static void CreatePropertyOnType(TypeBuilder typeBuilder, string propertyName, Type propertyType)
{