/// <summary>Or操作</summary> /// <param name="exp"></param> /// <returns></returns> public WhereExpression Or(Expression exp) { if (exp != null) { Expressions.Add(new ExpItem(false, exp)); } return this; }
/// <summary>And操作</summary> /// <param name="exp"></param> /// <returns></returns> public WhereExpression And(Expression exp) { if (exp != null) { // 如果前面有Or,则整体推入下一层 if (Expressions.Any(e => !e.IsAnd)) { var where = new WhereExpression(); where.Expressions.AddRange(Expressions); Expressions.Clear(); Expressions.Add(new ExpItem(true, where)); } Expressions.Add(new ExpItem(true, exp)); } return this; }
static WhereExpression And(Expression exp, Expression value) { // 如果exp为空,主要考虑右边 if (exp == null) CreateWhere(value); // 左边构造条件表达式,自己是也好,新建立也好 var where = CreateWhere(exp); if (value == null) return where; // 如果右边为空,创建的表达式将会失败,直接返回左边 return where.And(value); }
static WhereExpression CreateWhere(Expression value) { if (value == null) return new WhereExpression(); if (value is WhereExpression) return (value as WhereExpression); return new WhereExpression(value); }
/// <summary>把普通表达式包装为条件表达式表达式</summary> /// <param name="exp"></param> public WhereExpression(Expression exp) { And(exp); }
public ExpItem(Boolean isAnd, Expression exp) { IsAnd = isAnd; Exp = exp; }
/// <summary>有条件Or操作</summary> /// <param name="condition"></param> /// <param name="exp"></param> /// <returns></returns> public WhereExpression OrIf(Boolean condition, Expression exp) { return condition ? Or(exp) : this; }
/// <summary>有条件And操作</summary> /// <param name="condition"></param> /// <param name="exp"></param> /// <returns></returns> public WhereExpression AndIf(Boolean condition, Expression exp) { return condition ? And(exp) : this; }