日期:2014-05-17 浏览次数:21092 次
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>> where3 = l => l.PVType == 4
&& l.PVGuid == "1234"
&& l.PVIP == "127.0.0.1";
var count = Factory.CreateInstance.PVHistoryDao.NHibernateSession.Linq<Model.Pub.PVHistory>().Where(where3).Count();
using System.Linq;
using System.Linq.Expressions;
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.ToArray());
var right = Expression.Invoke(expr2, p.ToArray());
var expr = Expression.And(left, right);
return Expression.Lambda<TFunc>(expr, p);
}
}
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>> where1 = l => l.PVType == 4;
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>> where2 = l => l.PVGuid == "1234";
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>> where3 = l => l.PVIP == "127.0.0.1";
var where = where1.And(where2).And(where3);
var count = Factory.CreateInstance.PVHistoryDao.NHibernateSession.Linq<Model.Pub.PVHistory>().Where(where).Count();