如:
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>> where = l
=> l.PVType == 4
&& l.PVTarget == "Guest";
var count = Factory.CreateInstance.PVHistoryDao.Count(where);
如何拆成
System.Linq.Expressions.Expression<Func<Model.Pub.PVHistory, bool>>
where = l => l.PVType == 4;
where +=l =>l.PVTarget == "Guest";
像这种多行的写法
谢谢
------最佳解决方案--------------------------------------------------------
写一个扩展方法:
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);
------其他解决方案--------------------------------------------------------
...)
------其他解决方案--------------------------------------------------------
其实就是:
我要把
=> l.PVType == 4
&& l.PVTarget == "Guest";
这一句话拆分成两句来写
谢谢
------其他解决方案--------------------------------------------------------
query = xxxx
query = query.Where(...)
query = query.Where(...)
相当于query = xxx.Where(... && ...)
类似的
query = xxxx
query1 = query.Where(...)
query2 = query.Where(...)
query = query1.Union(query2)
相当于query = xxx.Where(...
------其他解决方案--------------------------------------------------------
query.Where(n=>...).Where(n=>...).Count()