This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parameter = Expression.Parameter(typeof(Product), "x"); | |
var property = Expression.Property(parameter, "Name") | |
// Find single parameter 'Contains' method of string with reflection: | |
var method = typeof(string).GetMethod(nameof(string.Contains), types: new[] { typeof(string) }); | |
var comparison = Expression.Call( | |
method: method, | |
instance: property | |
arguments: new []{ Expression.Constant(filter.Name) }); | |
var lambda = Expression.Lambda<Func<Product, bool>>(comparison, parameter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var query = db.Products.Where(x => x.Stock > 0 && x.Stock < 9); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parameter = Expression.Parameter(typeof(Product), "x"); | |
var property = Expression.Property(parameter, "Stock"); | |
var comparisonGt = Expression.GreaterThan(property, Expression.Constant(0)); | |
var comparisonLt = Expression.LessThan(property, Expression.Constant(9)); | |
var comparison = Expression.And(comparisonGt, comparisonLt); | |
var lambda = Expression.Lambda<Func<Product,bool>>(comparison, parameter); | |
var query = db.Products.Where(lambda); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFilter | |
{ | |
Expression BuildExpression(Type entityType, Expression body); | |
IQueryable<TEntity> ApplyFilterTo<TEntity>(IQueryable<TEntity> query); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FilterBase : IFilter | |
{ | |
public virtual CombineType CombineWith { get; set; } | |
public virtual IQueryable<TEntity> ApplyFilterTo<TEntity>(IQueryable<TEntity> query) | |
{ | |
var parameter = Expression.Parameter(typeof(TEntity), "x"); | |
var exp = BuildExpression(typeof(TEntity), parameter); | |
if (exp == null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFilterableType | |
{ | |
Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | |
public abstract class FilteringOptionsBaseAttribute : Attribute, IFilterableType | |
{ | |
public abstract Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | |
public class StringFilterOptionsAttribute : FilteringOptionsBaseAttribute | |
{ | |
public StringFilterOptionsAttribute(StringFilterOption option) | |
{ | |
this.Option = option; | |
} | |
public StringFilterOption Option { get; set; } | |
public StringComparison StringComparison { get; set; } = StringComparison.InvariantCultureIgnoreCase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Flags] | |
public enum StringFilterOption | |
{ | |
Equals = 0, | |
StartsWith = 1 << 0, | |
EndsWith = 1 << 1, | |
Contains = 1 << 2, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRange<T> : IFilterableType where T :struct, IComparable | |
{ | |
T? Min { get; set; } | |
T? Max { get; set; } | |
} |