-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathDictionary.cs
More file actions
114 lines (96 loc) · 4.42 KB
/
Dictionary.cs
File metadata and controls
114 lines (96 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using NFluent;
using Xunit;
namespace System.Linq.Dynamic.Core.Tests.MikArea
{
public class Dictionary
{
public class Customer
{
public string City { get; set; }
public Dictionary<string, Order> Orders { get; set; }
public string CompanyName { get; set; }
public string Phone { get; set; }
}
public class Order
{
}
[Fact]
public void Test_ContainsKey_1()
{
List<Customer> customers = new List<Customer>()
{
new Customer() { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST", new Order()));
var data = customers.AsQueryable()
.Where("Orders.ContainsKey(\"ZZZ2TEST\")")
.OrderBy("CompanyName")
.Select("new(City as City, Phone)").ToDynamicList();
Check.That("ZZZ2").IsEqualTo((string)data.First().City);
}
[Fact]
public void Test_ContainsKey_2()
{
List<Customer> customers = new List<Customer>()
{
new Customer() { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST", new Order()));
var data = customers.AsQueryable()
.Where("Orders.ContainsKey(it.City + \"TEST\")")
.OrderBy("City")
.Select("new(City as City, Phone)").ToDynamicList();
Check.That("ZZZ1").IsEqualTo((string)data.First().City);
Check.That(3).IsEqualTo(data.Count);
}
[Fact]
public void Test_ContainsKey_3()
{
List<Customer> customers = new List<Customer>()
{
new Customer() { City = "ZZZ1", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ2", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() },
new Customer() { City = "ZZZ3", CompanyName = "ZZZ", Orders = new Dictionary<string, Order>() }
};
customers.ForEach(x => x.Orders.Add(x.City + "TEST1", new Order()));
customers.ForEach(x => x.Orders.Add(x.City + "TEST2", new Order()));
var noDynamicList = customers
.Where(x => x.Orders.Skip(1).First().Key == (x.City + "TEST2"))
.OrderBy(x => x.City)
.ToList();
var data = customers.AsQueryable()
.Where("Orders.Skip(1).First().Key == (it.City + \"TEST2\")")
.OrderBy("City")
.Select("new(City as City, Phone)").ToDynamicList();
Check.That(3).IsEqualTo(noDynamicList.Count);
Check.That("ZZZ1").IsEqualTo((string)data.First().City);
Check.That(3).IsEqualTo(data.Count);
}
[Fact]
public void Test_DynamicIndexCall()
{
{
object CreateDicParameter(string name) => new Dictionary<string, object>
{
{"Name", new Dictionary<string, object> {{"FirstName", name }, {"LastName", name + "Test" }}},
};
var parType = new Dictionary<string, object>().GetType();
var lambda = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(parType, "item") }, typeof(object), "item.Name.FirstName + \"7\" + item.Name.LastName ").Compile();
var x1 = lambda.DynamicInvoke(CreateDicParameter("Julio"));
var x2 = lambda.DynamicInvoke(CreateDicParameter("John"));
Check.That(x1).IsEqualTo("Julio7JulioTest");
Check.That(x2).IsEqualTo("John7JohnTest");
}
}
}
}