将VB.NET 的LINQ改成C#版本
PublicFunctionContainsExpression(OfTElement,TValue)(ByValvalueSelectorAsSystem.Linq.E...
Public Function ContainsExpression(Of TElement, TValue)(ByVal valueSelector As System.Linq.Expressions.Expression(Of Func(Of TElement, TValue)), ByVal values As IEnumerable(Of TValue)) As System.Linq.Expressions.Expression(Of Func(Of TElement, Boolean))
If valueSelector Is Nothing Then
Throw New ArgumentNullException("valueSelector")
End If
If values Is Nothing Then
Throw New ArgumentNullException("values")
End If
Dim p As System.Linq.Expressions.ParameterExpression = valueSelector.Parameters.Single()
If Not values.Any() Then Return Function(e) False
Dim equals = values.Select(Function(value) DirectCast(System.Linq.Expressions.Expression.Equal(valueSelector.Body, System.Linq.Expressions.Expression.Constant(value, GetType(TValue))), System.Linq.Expressions.Expression))
Dim body = equals.Aggregate(Function(accumulate, equal) System.Linq.Expressions.Expression.Or(accumulate, equal))
Return System.Linq.Expressions.Expression.Lambda(Of Func(Of TElement, Boolean))(body, p)
End Function 展开
If valueSelector Is Nothing Then
Throw New ArgumentNullException("valueSelector")
End If
If values Is Nothing Then
Throw New ArgumentNullException("values")
End If
Dim p As System.Linq.Expressions.ParameterExpression = valueSelector.Parameters.Single()
If Not values.Any() Then Return Function(e) False
Dim equals = values.Select(Function(value) DirectCast(System.Linq.Expressions.Expression.Equal(valueSelector.Body, System.Linq.Expressions.Expression.Constant(value, GetType(TValue))), System.Linq.Expressions.Expression))
Dim body = equals.Aggregate(Function(accumulate, equal) System.Linq.Expressions.Expression.Or(accumulate, equal))
Return System.Linq.Expressions.Expression.Lambda(Of Func(Of TElement, Boolean))(body, p)
End Function 展开
2个回答
展开全部
public System.Linq.Expressions.Expression<Func<TElement, bool>> ContainsExpression<TElement, TValue>(System.Linq.Expressions.Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (valueSelector == null)
{
throw new ArgumentNullException("valueSelector");
}
if (values == null)
{
throw new ArgumentNullException("values");
}
System.Linq.Expressions.ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
return e => false;
var @equals = values.Select(value => (System.Linq.Expressions.Expression)System.Linq.Expressions.Expression.Equal(valueSelector.Body, System.Linq.Expressions.Expression.Constant(value, typeof(TValue))));
var body = @equals.Aggregate((accumulate, equal) => System.Linq.Expressions.Expression.Or(accumulate, equal));
return System.Linq.Expressions.Expression.Lambda<Func<TElement, bool>>(body, p);
}
{
if (valueSelector == null)
{
throw new ArgumentNullException("valueSelector");
}
if (values == null)
{
throw new ArgumentNullException("values");
}
System.Linq.Expressions.ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
return e => false;
var @equals = values.Select(value => (System.Linq.Expressions.Expression)System.Linq.Expressions.Expression.Equal(valueSelector.Body, System.Linq.Expressions.Expression.Constant(value, typeof(TValue))));
var body = @equals.Aggregate((accumulate, equal) => System.Linq.Expressions.Expression.Or(accumulate, equal));
return System.Linq.Expressions.Expression.Lambda<Func<TElement, bool>>(body, p);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public System.Linq.Expressions.Expression<Func<TElement, bool>> ContainsExpression<TElement, TValue>(System.Linq.Expressions.Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (valueSelector == null)
{
throw new ArgumentNullException("valueSelector");
}
if (values == null)
{
throw new ArgumentNullException("values");
}
System.Linq.Expressions.ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
return e => false;
var @equals = values.Select(value => (System.Linq.Expressions.Expression)System.Linq.Expressions.Expression.Equal(valueSelector.Body, System.Linq.Expressions.Expression.Constant(value, typeof(TValue))));
var body = @equals.Aggregate((accumulate, equal) => System.Linq.Expressions.Expression.Or(accumulate, equal));
return System.Linq.Expressions.Expression.Lambda<Func<TElement, bool>>(body, p);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询