Skip to content

Commit 2821de8

Browse files
Improve the logic when no alias is found
Issue: #403
1 parent 77f1738 commit 2821de8

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

Z.Dynamic.Core.Lab/Program.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Dynamic.Core;
5+
using Newtonsoft.Json;
26

37
namespace Z.Dynamic.Core.Lab
48
{
59
class Program
610
{
711
static void Main(string[] args)
812
{
13+
var data = new List<object> {
14+
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
15+
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
16+
new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
17+
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
18+
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
19+
new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
20+
};
21+
var jsonString = JsonConvert.SerializeObject(data);
22+
var list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);
23+
24+
var groupList = list.AsQueryable().GroupBy("new (ItemCode, Flag)").ToDynamicList();
25+
26+
//var data = new List<object> {
27+
// new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
28+
// new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
29+
// new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
30+
// new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
31+
// new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
32+
// new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
33+
//};
34+
//var jsonString = JsonConvert.SerializeObject(data);
35+
//var list = JsonConvert.DeserializeObject<List<object>>(jsonString).ToList();
36+
//var groupList = list.Select("new (ItemCode, Flag)");
37+
938
Request_Dictionary.Execute();
1039
}
1140
}

Z.Dynamic.Core.Lab/Z.Dynamic.Core.Lab.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
910
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
1011
</ItemGroup>
1112

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,18 @@ Expression ParseNew()
13361336
{
13371337
if (!TryGetMemberName(expr, out propName))
13381338
{
1339-
throw ParseError(exprPos, Res.MissingAsClause);
1339+
if (expr is MethodCallExpression methodCallExpression
1340+
&& methodCallExpression.Arguments.Count == 1
1341+
&& methodCallExpression.Arguments[0] is ConstantExpression methodCallExpressionArgument
1342+
&& methodCallExpressionArgument.Type == typeof(string)
1343+
&& properties.All(x => x.Name != (string)methodCallExpressionArgument.Value))
1344+
{
1345+
propName = (string)methodCallExpressionArgument.Value;
1346+
}
1347+
else
1348+
{
1349+
throw ParseError(exprPos, Res.MissingAsClause);
1350+
}
13401351
}
13411352
}
13421353

test/System.Linq.Dynamic.Core.Tests/QueryableTests.GroupBy.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using NFluent;
1+
using System.Collections.Generic;
2+
using NFluent;
23
using System.Linq.Dynamic.Core.Exceptions;
34
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
45
using System.Reflection;
6+
using Newtonsoft.Json;
57
using Xunit;
68

79
namespace System.Linq.Dynamic.Core.Tests
@@ -63,5 +65,24 @@ public void GroupBy_Dynamic_Exceptions()
6365
Assert.Throws<ArgumentException>(() => qry.GroupBy("Id", ""));
6466
Assert.Throws<ArgumentException>(() => qry.GroupBy("Id", " "));
6567
}
68+
69+
[Fact]
70+
public void GroupBy_Dynamic_Issue403()
71+
{
72+
var data = new List<object> {
73+
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
74+
new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO02" } ,
75+
new { ItemCode = "AAAA", Flag = false, SoNo="aaa",JobNo="JNO03" } ,
76+
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO04" },
77+
new { ItemCode = "BBBB", Flag = true, SoNo="bbb",JobNo="JNO05" } ,
78+
new { ItemCode = "BBBB", Flag = true, SoNo="ccc",JobNo="JNO06" } ,
79+
};
80+
var jsonString = JsonConvert.SerializeObject(data);
81+
var list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString);
82+
83+
var groupList = list.AsQueryable().GroupBy("new (ItemCode, Flag)").ToDynamicList();
84+
85+
Assert.Equal(3, groupList.Count);
86+
}
6687
}
6788
}

0 commit comments

Comments
 (0)