日期:2014-05-17  浏览次数:21023 次

|ZYCWPF| NHibernate.Linq中的Where条件如何进行分开写 谢谢

            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();

如上是用一条Lambda写的Where条件,出来的结果是正确的,
但我想分开来写这个Where条件,所以用以下

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();


在NHibernate.Linq.Visitors 148行
case BinaryCriterionType.Value:
  return compareValueToCri