QueryExtensions IsIn / NotIsIn
Δημοσιεύτηκε: Τρί Φεβ 08, 2022 6:43 am
public static IQueryable<TElement> IsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, params TValue[] values) => source.Where(WhereInExpression(propertySelector, values));
public static IQueryable<TElement> IsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) => source.Where(WhereInExpression(propertySelector, values));
private static Expression<Func<TElement, bool>> WhereInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) {
var p = propertySelector.Parameters.Single();
var list = values as IList<TValue> ?? values.ToList();
if (!list.Any()) return x => false;
var e = list.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = e.Aggregate(Expression.Or);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
public static IQueryable<TElement> NotIsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, params TValue[] values) => source.Where(GetWhereNotInExpression(propertySelector, values));
public static IQueryable<TElement> NotIsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) => source.Where(GetWhereNotInExpression(propertySelector, values));
private static Expression<Func<TElement, bool>> GetWhereNotInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) {
var p = propertySelector.Parameters.Single();
var list = values as IList<TValue> ?? values.ToList();
if (!list.Any()) return x => false;
var e = list.Select(value => (Expression)Expression.NotEqual(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = e.Aggregate(Expression.And);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
public static IQueryable<TElement> IsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) => source.Where(WhereInExpression(propertySelector, values));
private static Expression<Func<TElement, bool>> WhereInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) {
var p = propertySelector.Parameters.Single();
var list = values as IList<TValue> ?? values.ToList();
if (!list.Any()) return x => false;
var e = list.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = e.Aggregate(Expression.Or);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
public static IQueryable<TElement> NotIsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, params TValue[] values) => source.Where(GetWhereNotInExpression(propertySelector, values));
public static IQueryable<TElement> NotIsIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) => source.Where(GetWhereNotInExpression(propertySelector, values));
private static Expression<Func<TElement, bool>> GetWhereNotInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) {
var p = propertySelector.Parameters.Single();
var list = values as IList<TValue> ?? values.ToList();
if (!list.Any()) return x => false;
var e = list.Select(value => (Expression)Expression.NotEqual(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = e.Aggregate(Expression.And);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}