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 namespace PoweredSoft.DynamicLinq.Test
{ {
internal class TestStructure
{
public long ClientId { get; set; }
public decimal B { get; set; }
}
[TestClass] [TestClass]
public class GroupingTests public class GroupingTests
{ {
@ -34,6 +40,11 @@ namespace PoweredSoft.DynamicLinq.Test
.AsQueryable() .AsQueryable()
.GroupBy(t => t.Path("ClientId").Path("NetSales", "B")); .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 .Select(t => new
{ {

View File

@ -86,7 +86,7 @@ namespace PoweredSoft.DynamicLinq
if (groupBuilder.Empty) if (groupBuilder.Empty)
throw new Exception("No group specified, please specify at least one group"); 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 class GroupBuilder
{ {
public List<(string path, string propertyName)> Parts { get; set; } = new List<(string path, string propertyName)>(); 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 bool Empty => !Parts.Any();
public GroupBuilder Path(string path, string propertyName = null) public GroupBuilder Path(string path, string propertyName = null)
@ -30,5 +31,11 @@ namespace PoweredSoft.DynamicLinq.Fluent
Parts.Add((path, propertyName)); Parts.Add((path, propertyName));
return this; return this;
} }
public GroupBuilder UseType(Type type)
{
Type = type;
return this;
}
} }
} }

View File

@ -77,7 +77,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
return ret; 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 // EXPRESSION
var parameter = Expression.Parameter(type, "t"); var parameter = Expression.Parameter(type, "t");
@ -93,7 +93,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
partExpressions.Add((partExpression, part.propertyName)); 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> /// <summary>
/// Use this to create anonymous type concstructor for LINQ queries :) /// Use this to create anonymous type
/// https://stackoverflow.com/questions/6879279/using-typebuilder-to-create-a-pass-through-constructor-for-the-base-class
/// </summary> /// </summary>
/// <param name="fields"></param> /// <param name="fields"></param>
/// <returns></returns> /// <returns></returns>
@ -50,11 +49,16 @@ namespace PoweredSoft.DynamicLinq.Helpers
{ {
CreatePropertyOnType(dynamicType, field.name, field.type); CreatePropertyOnType(dynamicType, field.name, field.type);
}); });
// not needed at the end.
// CreateConstructorWithAllPropsOnType(dynamicType, fields); // CreateConstructorWithAllPropsOnType(dynamicType, fields);
var ret = dynamicType.CreateTypeInfo(); var ret = dynamicType.CreateTypeInfo();
return ret; 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) 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()); 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.Call, ctor);
emitter.Emit(OpCodes.Ret); emitter.Emit(OpCodes.Ret);
} }*/
internal static void CreatePropertyOnType(TypeBuilder typeBuilder, string propertyName, Type propertyType) internal static void CreatePropertyOnType(TypeBuilder typeBuilder, string propertyName, Type propertyType)
{ {