public void FilterLogicalAnd() { var entityFilter = new EntityFilter { Filters = new List <EntityFilter> { new EntityFilter { Name = "Rank", Value = 7 }, new EntityFilter { Name = "Name", Value = "Blueberry" } } }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("(Rank == @0 and Name == @1)"); builder.Parameters.Count.Should().Be(2); builder.Parameters[0].Should().Be(7); builder.Parameters[1].Should().Be("Blueberry"); }
public static IQueryable <T> Filter <T>(this IQueryable <T> query, EntityFilter filter) { if (filter is null) { return(query); } if (query is null) { throw new ArgumentNullException(nameof(query)); } var builder = new LinqExpressionBuilder(); builder.Build(filter); var predicate = builder.Expression; var parameters = builder.Parameters.ToArray(); // nothing to filter if (string.IsNullOrWhiteSpace(predicate)) { return(query); } return(query.Where(predicate, parameters)); }
public void FilterLogicalOr() { var entityFilter = new EntityFilter { Logic = "or", Filters = new List <EntityFilter> { new EntityFilter { Name = "Rank", Value = 7 }, new EntityFilter { Name = "Name", Value = "Apple" } } }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("(Rank == @0 or Name == @1)"); builder.Parameters.Count.Should().Be(2); builder.Parameters[0].Should().Be(7); builder.Parameters[1].Should().Be("Apple"); }
public void FilterNull() { var builder = new LinqExpressionBuilder(); builder.Build(null); builder.Expression.Should().BeEmpty(); builder.Parameters.Count.Should().Be(0); }
public void FilterEmpty() { var entityFilter = new EntityFilter(); var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().BeEmpty(); builder.Parameters.Count.Should().Be(0); }
public void FilterNormal() { var entityFilter = new EntityFilter { Name = "Rank", Value = 7 }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("Rank == @0"); builder.Parameters.Count.Should().Be(1); builder.Parameters[0].Should().Be(7); }
public static IQueryable <T> Filter <T>(this IQueryable <T> query, EntityFilter filter) { if (filter == null) { return(query); } var builder = new LinqExpressionBuilder(); builder.Build(filter); var predicate = builder.Expression; var parameters = builder.Parameters.ToArray(); return(query.Where(predicate, parameters)); }
public void FilterNotEndsWith() { var entityFilter = new EntityFilter { Name = "Name", Operator = "!EndsWith", Value = "berry" }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("!Name.EndsWith(@0)"); builder.Parameters.Count.Should().Be(1); builder.Parameters[0].Should().Be("berry"); }
public void FilterIn() { var entityFilter = new EntityFilter { Name = "Name", Operator = "in", Value = new [] { "Test", "Tester" } }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("it.Name in @0"); builder.Parameters.Count.Should().Be(1); builder.Parameters[0].Should().BeOfType <string[]>(); }
public void FilterContains() { var entityFilter = new EntityFilter { Name = "Name", Operator = "Contains", Value = "Berry" }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("Name.Contains(@0)"); builder.Parameters.Count.Should().Be(1); builder.Parameters[0].Should().Be("Berry"); }
public void FilterComplex() { var entityFilter = new EntityFilter { Filters = new List <EntityFilter> { new EntityFilter { Name = "Rank", Operator = ">", Value = 5 }, new EntityFilter { Logic = "or", Filters = new List <EntityFilter> { new EntityFilter { Name = "Name", Value = "Strawberry" }, new EntityFilter { Name = "Name", Value = "Blueberry" } } } } }; var builder = new LinqExpressionBuilder(); builder.Build(entityFilter); builder.Expression.Should().NotBeEmpty(); builder.Expression.Should().Be("(Rank > @0 and (Name == @1 or Name == @2))"); builder.Parameters.Count.Should().Be(3); builder.Parameters[0].Should().Be(5); builder.Parameters[1].Should().Be("Strawberry"); builder.Parameters[2].Should().Be("Blueberry"); }