|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Linq.Dynamic.Core; |
| 4 | +using System.Threading; |
| 5 | +using ConsoleAppEF2.Database; |
| 6 | + |
| 7 | +namespace ConsoleAppEF5 |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + var users = new[] { new User { FirstName = "Doe" } }.AsQueryable(); |
| 14 | + |
| 15 | + var context = new TestContext(); |
| 16 | + |
| 17 | + //context.Database.EnsureDeleted(); |
| 18 | + //context.Database.EnsureCreated(); |
| 19 | + |
| 20 | + var dateDeleted = new DateTime(2019, 2, 2); |
| 21 | + |
| 22 | + var dateLastModified = new DateTime(2018, 1, 15); |
| 23 | + if (!context.Cars.Any()) |
| 24 | + { |
| 25 | + context.Cars.Add(new Car { Brand = "Ford", Color = "Blue", Vin = "yes", Year = "2017", Extra = "e1", NullableInt = 1, DateLastModified = dateLastModified, DateDeleted = dateDeleted }); |
| 26 | + context.Cars.Add(new Car { Brand = "Fiat", Color = "Red", Vin = "yes", Year = "2016", DateLastModified = dateLastModified.AddDays(1) }); |
| 27 | + context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "no", Year = "1979", Extra = "e2", NullableInt = 2, DateLastModified = dateLastModified.AddDays(2) }); |
| 28 | + context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "a%bc", Year = "1979", Extra = "e3", NullableInt = 3, DateLastModified = dateLastModified.AddDays(3), DateDeleted = dateDeleted.AddDays(1) }); ; |
| 29 | + context.Cars.Add(new Car { Brand = "Ford", Color = "Yellow", Vin = "no", Year = "2020", DateLastModified = dateLastModified }); |
| 30 | + context.SaveChanges(); |
| 31 | + } |
| 32 | + |
| 33 | + context = new TestContext(); |
| 34 | + var orderByYear = context.Cars.OrderBy("Year DESC").ToList(); |
| 35 | + foreach (var x in orderByYear) |
| 36 | + { |
| 37 | + Console.WriteLine($"orderBy Year DESC = {x.Brand}"); |
| 38 | + } |
| 39 | + |
| 40 | + Thread.Sleep(500); |
| 41 | + |
| 42 | + context = new TestContext(); |
| 43 | + var orderBy3 = context.Cars.OrderBy("3 DESC").ToList(); |
| 44 | + foreach (var x in orderBy3) |
| 45 | + { |
| 46 | + Console.WriteLine($"orderBy 3 DESC = {x.Brand}"); |
| 47 | + } |
| 48 | + |
| 49 | + var contains = context.Cars.Where("Brand.Contains(@0)", "a").ToDynamicList(); |
| 50 | + |
| 51 | + var npExtra1 = context.Cars.Select("np(Extra, \"no-extra\")").ToDynamicList(); |
| 52 | + var npExtra2 = context.Cars.Select("np(Extra, string.Empty)").ToDynamicList(); |
| 53 | + var npExtra3 = context.Cars.Any("np(Extra, string.Empty).ToUpper() == \"e1\""); |
| 54 | + |
| 55 | + var npNullableInt = context.Cars.Select("np(NullableInt, 42)").ToDynamicList(); |
| 56 | + |
| 57 | + var selectNullableDateTime = context.Cars.FirstOrDefault(c => c.DateDeleted == dateDeleted); |
| 58 | + Console.WriteLine($"selectNullableDateTime.Key = {selectNullableDateTime.Key}"); |
| 59 | + |
| 60 | + var orderByNullableDateTimeResult = context.Cars.OrderBy(c => c.DateDeleted); |
| 61 | + foreach (var x in orderByNullableDateTimeResult) |
| 62 | + { |
| 63 | + Console.WriteLine($"orderByNullableDateTimeResult.Key,DateDeleted = {x.Key},{x.DateDeleted}"); |
| 64 | + } |
| 65 | + |
| 66 | + Console.WriteLine(new string('-', 80)); |
| 67 | + |
| 68 | + var orderByNullableDateTimeDynamicResult = context.Cars.OrderBy("DateDeleted"); |
| 69 | + foreach (var x in orderByNullableDateTimeDynamicResult) |
| 70 | + { |
| 71 | + Console.WriteLine($"orderByNullableDateTimeDynamicResult.Key,DateDeleted = {x.Key},{x.DateDeleted}"); |
| 72 | + } |
| 73 | + |
| 74 | + Console.WriteLine(new string('-', 80)); |
| 75 | + var orderByNullableDateTimeDynamicResultNew = context.Cars.Select("new (Color, DateDeleted)").OrderBy("DateDeleted desc"); |
| 76 | + foreach (dynamic x in orderByNullableDateTimeDynamicResultNew) |
| 77 | + { |
| 78 | + Console.WriteLine($"orderByNullableDateTimeDynamicResult2.Color,DateDeleted = {x.Color},{x.DateDeleted}"); |
| 79 | + } |
| 80 | + |
| 81 | + var config = new ParsingConfig { AllowNewToEvaluateAnyType = true, ResolveTypesBySimpleName = false }; |
| 82 | + var select = context.Cars.Select<Car>(config, $"new {typeof(Car).FullName}(it.Key as Key, \"?\" as Brand, string(null) as Color, string(\"e\") as Extra)"); |
| 83 | + foreach (Car car in select) |
| 84 | + { |
| 85 | + Console.WriteLine($"{car.Key}"); |
| 86 | + } |
| 87 | + |
| 88 | + var resultDynamic = users.Any("c => np(c.FirstName, string.Empty).ToUpper() == \"DOE\""); |
| 89 | + Console.WriteLine(resultDynamic); |
| 90 | + |
| 91 | + var users2 = users.Select<User>(config, "new User(it.FirstName as FirstName, 1 as Field)"); |
| 92 | + foreach (User u in users2) |
| 93 | + { |
| 94 | + Console.WriteLine($"u.FirstName = {u.FirstName}, u.Field = {u.Field}"); |
| 95 | + } |
| 96 | + |
| 97 | + try |
| 98 | + { |
| 99 | + users.Select<User>(config, "new User(1 as FieldDoesNotExist)"); |
| 100 | + } |
| 101 | + catch (Exception e) |
| 102 | + { |
| 103 | + Console.WriteLine(e); |
| 104 | + } |
| 105 | + |
| 106 | + foreach (dynamic x in users.Select("new (FirstName, string(\"a\") as StrA, string('c') as StrCh, string(\"\") as StrEmpty1, string('\0') as StrEmpty2, string(null) as StrNull)")) |
| 107 | + { |
| 108 | + Console.WriteLine($"x.FirstName = '{x.FirstName}' ; x.Str = '{x.Str == null}'"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public class User |
| 113 | + { |
| 114 | + public string FirstName { get; set; } |
| 115 | + public string LastName { get; set; } |
| 116 | + public string EmailAddress { get; set; } |
| 117 | + |
| 118 | + public int Field; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments