|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Dynamic; |
| 4 | +using System.Linq; |
| 5 | +using System.Linq.Dynamic.Core; |
| 6 | +using System.Linq.Dynamic.Core.CustomTypeProviders; |
| 7 | +using ConsoleAppEF2.Database; |
| 8 | +using Microsoft.EntityFrameworkCore; |
| 9 | +using Newtonsoft.Json; |
| 10 | + |
| 11 | +namespace ConsoleAppEF2 |
| 12 | +{ |
| 13 | + class Program |
| 14 | + { |
| 15 | + class C : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider |
| 16 | + { |
| 17 | + public HashSet<Type> GetCustomTypes() |
| 18 | + { |
| 19 | + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| 20 | + |
| 21 | + var set = new HashSet<Type>(FindTypesMarkedWithDynamicLinqTypeAttribute(assemblies)) |
| 22 | + { |
| 23 | + typeof(TestContext) |
| 24 | + }; |
| 25 | + |
| 26 | + return set; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private static IQueryable GetQueryable() |
| 31 | + { |
| 32 | + var random = new Random((int)DateTime.Now.Ticks); |
| 33 | + |
| 34 | + var x = Enumerable.Range(0, 10).Select(i => new |
| 35 | + { |
| 36 | + Id = i, |
| 37 | + Value = random.Next(), |
| 38 | + }); |
| 39 | + |
| 40 | + return x.AsQueryable().Select("new (it as Id, @0 as Value)", random.Next()); |
| 41 | + // return x.AsQueryable(); //x.AsQueryable().Select("new (Id, Value)"); |
| 42 | + } |
| 43 | + |
| 44 | + static void Main(string[] args) |
| 45 | + { |
| 46 | + IQueryable qry = GetQueryable(); |
| 47 | + |
| 48 | + var result = qry.Select("it").OrderBy("Value"); |
| 49 | + try |
| 50 | + { |
| 51 | + Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented)); |
| 52 | + } |
| 53 | + catch (Exception) |
| 54 | + { |
| 55 | + // Console.WriteLine(e); |
| 56 | + } |
| 57 | + |
| 58 | + var all = new |
| 59 | + { |
| 60 | + test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)), |
| 61 | + test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)), |
| 62 | + test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int)) |
| 63 | + }; |
| 64 | + Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented)); |
| 65 | + |
| 66 | + var config = new ParsingConfig |
| 67 | + { |
| 68 | + CustomTypeProvider = new C() |
| 69 | + }; |
| 70 | + |
| 71 | + var context = new TestContext(); |
| 72 | + context.Cars.Add(new Car { Brand = "Ford", Color = "Blue", Vin = "yes", Year = "2017" }); |
| 73 | + context.Cars.Add(new Car { Brand = "Fiat", Color = "Red", Vin = "yes", Year = "2016" }); |
| 74 | + context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "no", Year = "1979" }); |
| 75 | + context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "a%bc", Year = "1979" }); |
| 76 | + context.SaveChanges(); |
| 77 | + |
| 78 | + var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\""); |
| 79 | + Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented)); |
| 80 | + |
| 81 | + var carsLike1 = |
| 82 | + from c in context.Cars |
| 83 | + where EF.Functions.Like(c.Brand, "%a%") |
| 84 | + select c; |
| 85 | + Console.WriteLine("carsLike1 {0}", JsonConvert.SerializeObject(carsLike1, Formatting.Indented)); |
| 86 | + |
| 87 | + var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%")); |
| 88 | + Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented)); |
| 89 | + |
| 90 | + var dynamicCarsLike1 = context.Cars.Where(config, "TestContext.Like(Brand, \"%a%\")"); |
| 91 | + Console.WriteLine("dynamicCarsLike1 {0}", JsonConvert.SerializeObject(dynamicCarsLike1, Formatting.Indented)); |
| 92 | + |
| 93 | + var dynamicCarsLike2 = context.Cars.Where(config, "TestContext.Like(Brand, \"%d%\")"); |
| 94 | + Console.WriteLine("dynamicCarsLike2 {0}", JsonConvert.SerializeObject(dynamicCarsLike2, Formatting.Indented)); |
| 95 | + |
| 96 | + var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")"); |
| 97 | + Console.WriteLine("dynamicFunctionsLike1 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented)); |
| 98 | + |
| 99 | + var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")"); |
| 100 | + Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented)); |
| 101 | + |
| 102 | + var testDynamic = context.Cars.Select(c => new |
| 103 | + { |
| 104 | + K = c.Key, |
| 105 | + C = c.Color |
| 106 | + }); |
| 107 | + |
| 108 | + var testDynamicResult = testDynamic.Select("it").OrderBy("C"); |
| 109 | + try |
| 110 | + { |
| 111 | + Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented)); |
| 112 | + } |
| 113 | + catch (Exception e) |
| 114 | + { |
| 115 | + Console.WriteLine(e); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments