|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using NFluent; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace System.Linq.Dynamic.Core.Tests.MikArea |
| 11 | +{ |
| 12 | + |
| 13 | + public class Comparer |
| 14 | + { |
| 15 | + public class Customer |
| 16 | + { |
| 17 | + public string City { get; set; } |
| 18 | + public List<Order> Orders { get; set; } |
| 19 | + public string CompanyName { get; set; } |
| 20 | + public string Phone { get; set; } |
| 21 | + } |
| 22 | + public class Order |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void Comparer_OrderBy_And_ThenBy() |
| 28 | + { |
| 29 | + List<Customer> customers = new List<Customer>() { new Customer() { CompanyName = "Ååå"} , |
| 30 | + new Customer() { CompanyName = "Bbb" } , |
| 31 | + new Customer() { CompanyName = "Ååå" } , |
| 32 | + new Customer() { CompanyName = "Bbb" } , |
| 33 | + new Customer() { CompanyName = "Aaa" }, |
| 34 | + new Customer() { CompanyName = "Aaa" }, |
| 35 | + }; |
| 36 | + |
| 37 | + CultureInfo culture = new CultureInfo("nb-NO"); |
| 38 | + var test1 = customers.AsQueryable().OrderBy(x => x.CompanyName, StringComparer.Create(culture, true)).ToList(); |
| 39 | + var test2 = customers.AsQueryable().OrderBy(x => x.CompanyName).ToList(); |
| 40 | + var test3 = customers.AsQueryable().OrderBy(x => x.City).ThenBy(x => x.CompanyName, StringComparer.Create(culture, true)).ToList(); |
| 41 | + var test2_V2 = customers.AsQueryable() |
| 42 | + .OrderBy("CompanyName"); |
| 43 | + var test3_V2 = customers.AsQueryable() |
| 44 | + .OrderBy("City").ThenBy("CompanyName", StringComparer.Create(culture, true)); |
| 45 | + var test1_V2 = customers.AsQueryable() |
| 46 | + .OrderBy("CompanyName", StringComparer.Create(culture, true)); |
| 47 | + |
| 48 | + for (int i = 0; i < test2.Count; i++) |
| 49 | + { |
| 50 | + Check.That(test2.ElementAt(i).CompanyName).IsEqualTo(test2_V2.ElementAt(i).CompanyName); |
| 51 | + } |
| 52 | + for (int i = 0; i < test1.Count; i++) |
| 53 | + { |
| 54 | + Check.That(test1.ElementAt(i).CompanyName).IsEqualTo(test1_V2.ElementAt(i).CompanyName); |
| 55 | + } |
| 56 | + for (int i = 0; i < test3.Count; i++) |
| 57 | + { |
| 58 | + Check.That(test3.ElementAt(i).CompanyName).IsEqualTo(test3_V2.ElementAt(i).CompanyName); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void Comparer_GroupBy() |
| 64 | + { |
| 65 | + List<Customer> customers = new List<Customer>() { new Customer() { City = "ZZZ" }, new Customer() { City = "ZzZ" } }; |
| 66 | + var check = customers.GroupBy(x => x.City, StringComparer.InvariantCultureIgnoreCase).ToList(); |
| 67 | + var check2 = customers.GroupBy(x => x.City,x => new { Name = x.CompanyName, Phone = x.Phone }, StringComparer.InvariantCultureIgnoreCase).ToList(); |
| 68 | + var check3 = customers.GroupBy(x => x.City, x => new { Name = x.CompanyName, Phone = x.Phone }).ToList(); |
| 69 | + |
| 70 | + var check_V2 = customers.AsQueryable().GroupBy("City", StringComparer.InvariantCultureIgnoreCase); |
| 71 | + var check2_V2 = customers.AsQueryable().GroupBy("City", "new(CompanyName as Name, Phone)", StringComparer.InvariantCultureIgnoreCase); |
| 72 | + var check3_V2 = customers.AsQueryable().GroupBy("City", "new(CompanyName as Name, Phone)"); |
| 73 | + |
| 74 | + Check.That(check.Count).IsEqualTo(check_V2.Count()); |
| 75 | + Check.That(check2.Count).IsEqualTo(check2_V2.Count()); |
| 76 | + Check.That(check3.Count).IsEqualTo(check3_V2.Count()); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments