Skip to content

Commit

Permalink
Improve the logic when no alias is found
Browse files Browse the repository at this point in the history
Issue: #403
  • Loading branch information
JonathanMagnan committed Aug 6, 2020
1 parent 77f1738 commit 2821de8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
29 changes: 29 additions & 0 deletions Z.Dynamic.Core.Lab/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using Newtonsoft.Json;

namespace Z.Dynamic.Core.Lab
{
class Program
{
static void Main(string[] args)
{
var data = new List<object> {
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
};
var jsonString = JsonConvert.SerializeObject(data);
var list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);

var groupList = list.AsQueryable().GroupBy("new (ItemCode, Flag)").ToDynamicList();

//var data = new List<object> {
// new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
// new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
// new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
// new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
// new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
// new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
//};
//var jsonString = JsonConvert.SerializeObject(data);
//var list = JsonConvert.DeserializeObject<List<object>>(jsonString).ToList();
//var groupList = list.Select("new (ItemCode, Flag)");

Request_Dictionary.Execute();
}
}
Expand Down
1 change: 1 addition & 0 deletions Z.Dynamic.Core.Lab/Z.Dynamic.Core.Lab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
</ItemGroup>

Expand Down
13 changes: 12 additions & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,18 @@ Expression ParseNew()
{
if (!TryGetMemberName(expr, out propName))
{
throw ParseError(exprPos, Res.MissingAsClause);
if (expr is MethodCallExpression methodCallExpression
&& methodCallExpression.Arguments.Count == 1
&& methodCallExpression.Arguments[0] is ConstantExpression methodCallExpressionArgument
&& methodCallExpressionArgument.Type == typeof(string)
&& properties.All(x => x.Name != (string)methodCallExpressionArgument.Value))
{
propName = (string)methodCallExpressionArgument.Value;
}
else
{
throw ParseError(exprPos, Res.MissingAsClause);
}
}
}

Expand Down
23 changes: 22 additions & 1 deletion test/System.Linq.Dynamic.Core.Tests/QueryableTests.GroupBy.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using NFluent;
using System.Collections.Generic;
using NFluent;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
using System.Reflection;
using Newtonsoft.Json;
using Xunit;

namespace System.Linq.Dynamic.Core.Tests
Expand Down Expand Up @@ -63,5 +65,24 @@ public void GroupBy_Dynamic_Exceptions()
Assert.Throws<ArgumentException>(() => qry.GroupBy("Id", ""));
Assert.Throws<ArgumentException>(() => qry.GroupBy("Id", " "));
}

[Fact]
public void GroupBy_Dynamic_Issue403()
{
var data = new List<object> {
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
};
var jsonString = JsonConvert.SerializeObject(data);
var list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);

var groupList = list.AsQueryable().GroupBy("new (ItemCode, Flag)").ToDynamicList();

Assert.Equal(3, groupList.Count);
}
}
}

0 comments on commit 2821de8

Please sign in to comment.