Skip to content

Instantly share code, notes, and snippets.

@enisn
Created July 21, 2020 07:12
Show Gist options
  • Save enisn/0d1c0742e712e46d8596b6beb9722c6c to your computer and use it in GitHub Desktop.
Save enisn/0d1c0742e712e46d8596b6beb9722c6c to your computer and use it in GitHub Desktop.
Array from dto search in db
using AutoFilterer.Attributes;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace AutoFilterer.Types
{
public class ArraySearchFilteringAttribute : FilteringOptionsBaseAttribute
{
public override Expression BuildExpression(Expression expressionBody, PropertyInfo targetProperty, PropertyInfo filterProperty, object value)
{
// db.Books.Where(x => value.Contains(x.TotalPage))
var type = targetProperty.PropertyType;
var prop = Expression.Property(expressionBody, targetProperty.Name);
var containsMethod = typeof(Enumerable).GetMethods().FirstOrDefault(x => x.Name == nameof(Enumerable.Contains)).MakeGenericMethod(type);
var containsExpression = Expression.Call(
method: containsMethod,
arguments: new Expression[]
{
Expression.Constant(value),
Expression.Property(expressionBody, targetProperty.Name)
});
return containsExpression;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment