public void StringTest(int collectionSize) { var kvps = new KeyValuePair <string, string> [collectionSize]; Random rand1 = new Random(); for (int i = 0; i < collectionSize; i++) { kvps[i] = new KeyValuePair <string, string>($"key{i}", Guid.NewGuid().ToString() + Guid.NewGuid() + Guid.NewGuid()); } string value1 = kvps[rand1.Next(0, collectionSize - 1)].Value; string value2 = kvps[rand1.Next(0, collectionSize - 1)].Value; string queryString = $"value={value1}%2B{value2}"; IQueryable <KeyValuePair <string, string> > queryable = kvps.AsQueryable(); var sw = Stopwatch.StartNew(); var expression = ExpressionBuilder.BuildExpression <KeyValuePair <string, string> >(queryString); queryable.FirstOrDefault(expression).Should().NotBeNull().And .Should().BeAnyOf(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2)); queryable.Where(expression).Should().NotBeNullOrEmpty().And .Contain(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2)); queryable.Any(expression).Should().BeTrue(); queryable.All(expression).Should().BeFalse(); queryable.Count(expression).Should().Be(queryable.Count(q => q.Value == value1 || q.Value == value2)); sw.Stop(); }
public void DateTimeTest(int collectionSize) { var kvps = new KeyValuePair <string, DateTime> [collectionSize]; int day = 1; int month = 1; int year = 2016; for (int i = 0; i < collectionSize; i++) { kvps[i] = new KeyValuePair <string, DateTime>($"key{i}", new DateTime(year, month, day++)); if (day > 28) { day = 1; month++; } if (month <= 12) { continue; } month = 1; year++; } Random rand = new Random(); DateTime value1 = kvps[rand.Next(0, collectionSize - 1)].Value; DateTime value2 = kvps[rand.Next(0, collectionSize - 1)].Value; string queryString = $"value={value1}%2B{value2}"; IQueryable <KeyValuePair <string, DateTime> > queryable = kvps.AsQueryable(); var sw = Stopwatch.StartNew(); var expression = ExpressionBuilder.BuildExpression <KeyValuePair <string, DateTime> >(queryString); queryable.FirstOrDefault(expression).Should().NotBeNull().And .Should().BeAnyOf(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2)); queryable.Where(expression).Should().NotBeNullOrEmpty().And .Contain(kvps.Where(kvp => kvp.Value == value1 || kvp.Value == value2)); queryable.Any(expression).Should().BeTrue(); queryable.All(expression).Should().BeFalse(); queryable.Count(expression).Should().Be(queryable.Count(q => q.Value == value1 || q.Value == value2)); sw.Stop(); }