-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathProgram.cs
165 lines (134 loc) · 6.93 KB
/
Program.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Reflection;
using System.Runtime.CompilerServices;
using ConsoleAppEF2.Database;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace ConsoleAppEF2
{
class Program
{
class C : AbstractDynamicLinqCustomTypeProvider, IDynamicLinqCustomTypeProvider
{
public C() : base(new List<Type>())
{
}
public HashSet<Type> GetCustomTypes()
{
// https://stackoverflow.com/a/2384679/255966
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var loadedPaths = loadedAssemblies.Where(a => !a.IsDynamic).Select(a => a.Location).ToArray();
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
return new HashSet<Type>(FindTypesMarkedWithDynamicLinqTypeAttribute(loadedAssemblies))
{
typeof(TestContext)
};
}
public Dictionary<Type, List<MethodInfo>> GetExtensionMethods()
{
var types = GetCustomTypes();
List<Tuple<Type, MethodInfo>> list = new List<Tuple<Type, MethodInfo>>();
foreach (var type in types)
{
var extensionMethods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Where(x => x.IsDefined(typeof(ExtensionAttribute), false)).ToList();
extensionMethods.ForEach(x => list.Add(new Tuple<Type, MethodInfo>(x.GetParameters()[0].ParameterType, x)));
}
return list.GroupBy(x => x.Item1, tuple => tuple.Item2).ToDictionary(key => key.Key, methods => methods.ToList());
}
public Type ResolveType(string typeName)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveType(assemblies, typeName);
}
public Type ResolveTypeBySimpleName(string typeName)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveTypeBySimpleName(assemblies, typeName);
}
}
private static IQueryable GetQueryable()
{
var random = new Random((int)DateTime.Now.Ticks);
var x = Enumerable.Range(0, 10).Select(i => new
{
Id = i,
Value = random.Next(),
});
return x.AsQueryable().Select("new (it as Id, @0 as Value)", random.Next());
// return x.AsQueryable(); //x.AsQueryable().Select("new (Id, Value)");
}
static void Main(string[] args)
{
IQueryable qry = GetQueryable();
var result = qry.Select("it").OrderBy("Value");
try
{
Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented));
}
catch (Exception)
{
// Console.WriteLine(e);
}
var all = new
{
test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
};
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
var config = new ParsingConfig();
config.CustomTypeProvider = new C();
var context = new TestContext();
// context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var dateLastModified = new DateTime(2018, 1, 15);
if (!context.Cars.Any())
{
context.Cars.Add(new Car { Brand = "Ford", Color = "Blue", Vin = "yes", Year = "2017", DateLastModified = dateLastModified });
context.Cars.Add(new Car { Brand = "Fiat", Color = "Red", Vin = "yes", Year = "2016", DateLastModified = dateLastModified.AddDays(1) });
context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "no", Year = "1979", DateLastModified = dateLastModified.AddDays(2) });
context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "a%bc", Year = "1979", DateLastModified = dateLastModified.AddDays(3) }); ;
context.SaveChanges();
}
var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
var carsLike1 =
from c in context.Cars
where EF.Functions.Like(c.Brand, "%a%")
select c;
Console.WriteLine("carsLike1 {0}", JsonConvert.SerializeObject(carsLike1, Formatting.Indented));
var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%"));
Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented));
var dynamicCarsLike1 = context.Cars.Where(config, "TestContext.Like(Brand, \"%a%\")");
Console.WriteLine("dynamicCarsLike1 {0}", JsonConvert.SerializeObject(dynamicCarsLike1, Formatting.Indented));
var dynamicCarsLike2 = context.Cars.Where(config, "TestContext.Like(Brand, \"%d%\")");
Console.WriteLine("dynamicCarsLike2 {0}", JsonConvert.SerializeObject(dynamicCarsLike2, Formatting.Indented));
var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")");
Console.WriteLine("dynamicFunctionsLike1 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented));
var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")");
Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
var testDynamic = context.Cars.Select(c => new
{
K = c.Key,
C = c.Color
});
var testDynamicResult = testDynamic.Select("it").OrderBy("C");
try
{
Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}