Entity expression error
I found this function (for a "where in" query expression) and I want to use it, but there are some errors and I don't know why because I saw this function in many forums and it should work...
static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector) { throw new ArgumentNullException("valueSelector"); }
if (null == values) { throw new ArgumentNullException("values"); }
ParameterExpression p = valueSelector.Parameters.Single();
//开发者_StackOverflow中文版 p => valueSelector(p) == values[0] || valueSelector(p) == ...
if (!values.Any())
{
return e => false;
}
var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
The errors are:
'System.Collections.ObjectModel.ReadOnlyCollection' does not contain a definition for 'Single' and no extension method 'Single' accepting a first argument of type 'System.Collections.ObjectModel.ReadOnlyCollection' could be found (are you missing a using directive or an assembly reference?)
'System.Collections.Generic.IEnumerable' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)
Can anyone help me? Thank you in advance.
You probably need to add:
using System.Linq;
精彩评论