public void SelectNullChecking() { var query = TestData.Authors.AsQueryable(); var qs = query.Select(t => new { CommentLikes = t.Posts == null ? new List <CommentLike>() : t.Posts.Where(t2 => t2.Comments != null).SelectMany(t2 => t2.Comments.Where(t3 => t3.CommentLikes != null).SelectMany(t3 => t3.CommentLikes)).ToList() }); var a = qs.ToList(); var querySelect = query.Select(t => { t.NullChecking(true); t.ToList("Posts.Comments.CommentLikes", selectCollectionHandling: SelectCollectionHandling.Flatten); }); var b = querySelect.ToDynamicClassList(); Assert.AreEqual(a.Count, b.Count); for (var i = 0; i < a.Count; i++) { var left = a[i]; var right = b[i]; var leftCommentLikes = left.CommentLikes; var rightCommentLikes = right.GetDynamicPropertyValue <List <CommentLike> >("CommentLikes"); QueryableAssert.AreEqual(leftCommentLikes.AsQueryable(), rightCommentLikes.AsQueryable()); } }
public void ContainsAndNotContains() { var q1 = Persons.AsQueryable().Query(t => t.Contains("LastName", "s").AndNotContains("LastName", "r")); var q1b = Persons.AsQueryable().Where(t => t.LastName.Contains("s") && !t.LastName.Contains("r")); QueryableAssert.AreEqual(q1, q1b); }
public void NotContains() { var q1 = Persons.AsQueryable().Query(t => t.Contains("LastName", "ee", negate: true)); var q1b = Persons.AsQueryable().Where(t => !t.LastName.Contains("ee")); QueryableAssert.AreEqual(q1, q1b); }
public void GreaterThanOrEqual(string firstName) { IQueryable <MockPersonObject> a, b; a = Persons.AsQueryable().Query(t => t.GreaterThanOrEqual("FirstName", firstName)); b = Persons.AsQueryable().Where(t => t.FirstName.CompareTo(firstName) >= 0); QueryableAssert.AreEqual(a, b); }
public void LessThan(string firstName) { IQueryable <MockPersonObject> a, b; a = Persons.AsQueryable().Query(t => t.LessThan("FirstName", firstName)); b = Persons.AsQueryable().Where(t => t.FirstName.CompareTo(firstName) < 0); QueryableAssert.AreEqual(a, b); }
public void TestSelect() { var list = new List <Mock>() { new Mock { Id = 1, ForeignId = 1, Total = 100, Bs = new List <MockB>() { new MockB { FirstNames = new List <string> { "David", "John" } } } }, }; var regularSyntaxA = list .AsQueryable() .Select(t => new { Id = t.Id, FirstNames = t.Bs.SelectMany(t2 => t2.FirstNames).ToList(), FirstNamesLists = t.Bs.Select(t2 => t2.FirstNames).ToList() }); var regularSyntax = regularSyntaxA.ToList(); var dynamicSyntax = list .AsQueryable() .Select(t => { t.Path("Id"); t.PathToList("Bs.FirstNames", "FirstNames", SelectCollectionHandling.Flatten); t.PathToList("Bs.FirstNames", "FirstNamesLists", SelectCollectionHandling.LeaveAsIs); }) .ToDynamicClassList(); Assert.AreEqual(regularSyntax.Count, dynamicSyntax.Count); for (var i = 0; i < regularSyntax.Count; i++) { Assert.AreEqual(regularSyntax[i].Id, dynamicSyntax[i].GetDynamicPropertyValue <int>("Id")); QueryableAssert.AreEqual(regularSyntax[i].FirstNames.AsQueryable(), dynamicSyntax[i].GetDynamicPropertyValue <List <string> >("FirstNames").AsQueryable()); var left = regularSyntax[i].FirstNamesLists; var right = dynamicSyntax[i].GetDynamicPropertyValue <List <List <string> > >("FirstNamesLists"); Assert.AreEqual(left.Count, right.Count); for (var j = 0; j < left.Count; j++) { QueryableAssert.AreEqual(left[j].AsQueryable(), right[j].AsQueryable()); } } }
public void MixingInComplexPaths() { var authorsFirstNames = new List <string>() { "David", "Pablo" }; var a = TestData.Posts.AsQueryable().Query(t => t.In("Author.FirstName", authorsFirstNames)); var b = TestData.Posts.AsQueryable().Where(t => authorsFirstNames.Contains(t.Author.FirstName)); QueryableAssert.AreEqual(a, b); }
public void MixingInWithCollectionPaths() { var titles = new List <string>() { "Match" }; var a = TestData.Authors.AsQueryable().Query(t => t.NullChecking(true).In("Posts.Title", titles)); var b = TestData.Authors.AsQueryable().Where(t => t.Posts != null && t.Posts.Any(t2 => titles.Contains(t2.Title))); QueryableAssert.AreEqual(a, b); }
public void NotIn() { IQueryable <MockPersonObject> a, b; var ageGroup = new List <int>() { 50, 58 }; a = TestData.Persons.AsQueryable().Query(t => t.NotIn("Age", ageGroup)); b = TestData.Persons.AsQueryable().Where(t => !ageGroup.Contains(t.Age)); QueryableAssert.AreEqual(a, b); }
public void InString() { IQueryable <MockPersonObject> a, b; var group = new List <string>() { "David", "Michaela" }; a = TestData.Persons.AsQueryable().Query(t => t.In("FirstName", group)); b = TestData.Persons.AsQueryable().Where(t => group.Contains(t.FirstName)); QueryableAssert.AreEqual(a, b); }
public void DiffTypeListConversion() { IQueryable <MockPersonObject> a, b; var ageGroup = new List <string>() { "28", "27", "50" }; var ageGroupInt = ageGroup.Select(t => Convert.ToInt32(t)).ToList(); a = TestData.Persons.AsQueryable().Query(t => t.In("Age", ageGroup)); b = TestData.Persons.AsQueryable().Where(t => ageGroupInt.Contains(t.Age)); QueryableAssert.AreEqual(a, b); }
public void NotEqual() { IQueryable <MockPersonObject> a, b; // case sensitive. a = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "David", stringComparision: null)); b = Persons.AsQueryable().Where(t => t.FirstName != "David"); QueryableAssert.AreEqual(a, b, "CaseSensitive"); // not case sensitive a = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "DAVID", stringComparision: StringComparison.OrdinalIgnoreCase)); b = Persons.AsQueryable().Where(t => !t.FirstName.Equals("DAVID", StringComparison.OrdinalIgnoreCase)); QueryableAssert.AreEqual(a, b, "CaseInsensitive"); }
public void StartsWith() { IQueryable <MockPersonObject> a, b; // case sensitive. a = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "Da", stringComparision: null)); b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("Da")); QueryableAssert.AreEqual(a, b, "CaseSensitive"); // not case sensitive a = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "DA", stringComparision: StringComparison.OrdinalIgnoreCase)); b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("DA", StringComparison.OrdinalIgnoreCase)); QueryableAssert.AreEqual(a, b, "CaseInsensitive"); }
public void NegateNotContains() { IQueryable <MockPersonObject> a, b; // case sensitive. a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "vi", stringComparision: null, negate: true)); b = Persons.AsQueryable().Where(t => !!t.FirstName.Contains("vi")); QueryableAssert.AreEqual(a, b, "CaseSensitive"); // not case sensitive a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "VI", stringComparision: StringComparison.OrdinalIgnoreCase, negate: true)); b = Persons.AsQueryable().Where(t => !(t.FirstName.IndexOf("VI", StringComparison.OrdinalIgnoreCase) == -1)); QueryableAssert.AreEqual(a, b, "CaseInsensitive"); }
public void LessThanOrEqual() { var q1 = Persons.AsQueryable().Query(t => t.LessThanOrEqual("Age", 50)); var q1b = Persons.AsQueryable().Where(t => t.Age <= 50); QueryableAssert.AreEqual(q1, q1b); var q2 = Persons.AsQueryable().Query(t => t.LessThanOrEqual("Age", 28).AndLessThanOrEqual("Age", 50)); var q2b = Persons.AsQueryable().Where(t => t.Age <= 28 && t.Age <= 50); QueryableAssert.AreEqual(q2, q2b); var q3 = Persons.AsQueryable().Query(t => t.LessThanOrEqual("Age", 28).OrLessThanOrEqual("Age", 50)); var q3b = Persons.AsQueryable().Where(t => t.Age <= 28 || t.Age <= 50); QueryableAssert.AreEqual(q3, q3b); }
public void GreatThanOrEqual() { var q1 = Persons.AsQueryable().Query(t => t.GreaterThanOrEqual("Age", 28)); var q1b = Persons.AsQueryable().Where(t => t.Age >= 28); QueryableAssert.AreEqual(q1, q1b); var q2 = Persons.AsQueryable().Query(t => t.GreaterThanOrEqual("Age", 28).AndGreaterThanOrEqual("Age", 50)); var q2b = Persons.AsQueryable().Where(t => t.Age >= 28 && t.Age >= 50); QueryableAssert.AreEqual(q2, q2b); var q3 = Persons.AsQueryable().Query(t => t.GreaterThanOrEqual("Age", 28).OrGreaterThanOrEqual("Age", 50)); var q3b = Persons.AsQueryable().Where(t => t.Age >= 28 || t.Age >= 50); QueryableAssert.AreEqual(q3, q3b); }
public void Contains() { var q1 = Persons.AsQueryable().Query(t => t.Contains("LastName", "ee")); var q1b = Persons.AsQueryable().Where(t => t.LastName.Contains("ee")); QueryableAssert.AreEqual(q1, q1b); var q2 = Persons.AsQueryable().Query(t => t.Contains("LastName", "ee").AndContains("FirstName", "vid")); var q2b = Persons.AsQueryable().Where(t => t.LastName.Contains("ee") && t.FirstName.Contains("vid")); QueryableAssert.AreEqual(q2, q2b); var q3 = Persons.AsQueryable().Query(t => t.Contains("LastName", "ee").OrContains("LastName", "ar")); var q3b = Persons.AsQueryable().Where(t => t.LastName.Contains("ee") || t.LastName.Contains("ar")); QueryableAssert.AreEqual(q3, q3b); }
public void StartsWith() { var q1 = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "Mi")); var q1b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("Mi")); QueryableAssert.AreEqual(q1, q1b); var q2 = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "Mi").AndStartsWith("LastName", "Vi")); var q2b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("Mi") && t.LastName.StartsWith("Vi")); QueryableAssert.AreEqual(q2, q2b); var q3 = Persons.AsQueryable().Query(t => t.StartsWith("FirstName", "Mi").OrStartsWith("FirstName", "Da")); var q3b = Persons.AsQueryable().Where(t => t.FirstName.StartsWith("Mi") || t.FirstName.StartsWith("Da")); QueryableAssert.AreEqual(q3, q3b); }
public void TestingSelectBuilderAggregateFluent() { var normalSyntax = TestData.Sales .GroupBy(t => new { t.ClientId }) .Select(t => new { TheClientId = t.Key.ClientId, Count = t.Count(), LongCount = t.LongCount(), NetSales = t.Sum(t2 => t2.NetSales), TaxAverage = t.Average(t2 => t2.Tax), Sales = t.ToList() }) .ToList(); var dynamicSyntax = TestData.Sales .AsQueryable() .GroupBy(t => t.Path("ClientId")) .Select(t => { t.Aggregate("Key.ClientId", SelectTypes.Key, "TheClientId"); // should not have to specify a path, but a property is a must t.Aggregate(null, SelectTypes.Count, "Count"); // support both ways it can use path to guess property so testing this too t.Aggregate("LongCount", SelectTypes.LongCount); t.Aggregate("NetSales", SelectTypes.Sum); t.Aggregate("Tax", SelectTypes.Average, "TaxAverage"); t.ToList("Sales"); }) .ToDynamicClassList(); Assert.AreEqual(normalSyntax.Count, dynamicSyntax.Count); for (var i = 0; i < normalSyntax.Count; i++) { var left = normalSyntax[i]; var right = dynamicSyntax[i]; Assert.AreEqual(left.TheClientId, right.GetDynamicPropertyValue("TheClientId")); Assert.AreEqual(left.Count, right.GetDynamicPropertyValue("Count")); Assert.AreEqual(left.LongCount, right.GetDynamicPropertyValue("LongCount")); Assert.AreEqual(left.TaxAverage, right.GetDynamicPropertyValue("TaxAverage")); QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue <List <MockSale> >("Sales").AsQueryable()); } }
public void WantedSyntax() { var normalSyntax = TestData.Sales .GroupBy(t => new { t.ClientId }) .Select(t => new { TheClientId = t.Key.ClientId, Count = t.Count(), LongCount = t.LongCount(), NetSales = t.Sum(t2 => t2.NetSales), TaxAverage = t.Average(t2 => t2.Tax), Sales = t.ToList() }) .ToList(); var dynamicSyntax = TestData.Sales .AsQueryable() .GroupBy(t => t.Path("ClientId")) .Select(t => { t.Key("TheClientId", "ClientId"); t.Count("Count"); t.LongCount("LongCount"); t.Sum("NetSales"); t.Average("Tax", "TaxAverage"); t.ToList("Sales"); }) .ToDynamicClassList(); Assert.AreEqual(normalSyntax.Count, dynamicSyntax.Count); for (var i = 0; i < normalSyntax.Count; i++) { var left = normalSyntax[i]; var right = dynamicSyntax[i]; Assert.AreEqual(left.TheClientId, right.GetDynamicPropertyValue("TheClientId")); Assert.AreEqual(left.Count, right.GetDynamicPropertyValue("Count")); Assert.AreEqual(left.LongCount, right.GetDynamicPropertyValue("LongCount")); Assert.AreEqual(left.TaxAverage, right.GetDynamicPropertyValue("TaxAverage")); QueryableAssert.AreEqual(left.Sales.AsQueryable(), right.GetDynamicPropertyValue <List <MockSale> >("Sales").AsQueryable()); } }
public void NotEqual() { // test simple var q1 = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "David")); var q1b = Persons.AsQueryable().Where(t => t.FirstName != "David"); QueryableAssert.AreEqual(q1, q1b); // test and var q2 = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "David").AndNotEqual("FirstName", "Michaela")); var q2b = Persons.AsQueryable().Where(t => t.FirstName != "David" && t.FirstName != "Michaela"); QueryableAssert.AreEqual(q2, q2b); // test or var q3 = Persons.AsQueryable().Query(t => t.NotEqual("FirstName", "David").OrNotEqual("LastName", "Lebee")); var q3b = Persons.AsQueryable().Where(t => t.FirstName != "David" || t.LastName != "Lebee"); QueryableAssert.AreEqual(q3, q3b); }