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