日期:2014-05-17 浏览次数:20473 次
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>> where = l
=> l.PVType == 4
&& l.PVTarget == "Guest";
var count = Factory.CreateInstance.PVHistoryDao.Count(where);
public static class LambdaExpressionExtensions
{
public static Expression<TFunc> And<TFunc>(this Expression<TFunc> expr1, Expression<TFunc> expr2)
{
if (expr1.ReturnType != typeof(bool)
------其他解决方案--------------------
expr2.ReturnType != typeof(bool))
throw new ArgumentException("both lambda expressions must return boolean type");
if (expr1.Parameters.Zip(expr2.Parameters, (p1, p2) => p1.Type == p2.Type).Any(x => x == false))
throw new ArgumentException("expr1 and expr2 must have exactly the same parameters");
var p = expr1.Parameters;
var left = Expression.Invoke(expr1, p);
var right = Expression.Invoke(expr2, p);
var expr = Expression.And(left, right);
return Expression.Lambda<TFunc>(expr, p);
}
}
Expression<Func<Model.Pub.PVHistory, bool>> where = l => l.PVType >= 4;
Expression<Func<Model.Pub.PVHistory, bool>> where2 = l=> l.PVTarget == "Guest";
where = where.And(where2);
var count = Factory.CreateInstance.PVHistoryDao.Count(where);