2016-11-09 90 views
1

中的条件我正在使用IQueryable来构建我的查询。如何在几个LINQ中执行'OR'操作LINQ

IQueryable<PropertyPosts> posts = context.PropertyPosts; 

之后,基于几个条件,我追加条款列入我的查询,

if (item.ApartmentCondo == 1) 
{ 
    posts= posts.Where(x => x.name == item.name && 
          x.PropertyType == PropertyType.ApartmentCondo); 

} 
if (item.House == 1) 
{ 
    posts= posts.Where(x => x.name == item.name && 
          x.PropertyType == PropertyType.House); 

} 

注意:还有其他一些地方的条件也。

后,当我执行以下查询,

List<PropertyPosts> posts2 = posts.ToList(); 

上述所有其中条件将是“AND后。

但相反,我需要在哪里条件'或'。

这意味着我需要一种方法来追加几个Where条件,但所有这些条件都应该在它们之间执行'OR'条件。

我该如何做到这一点?有没有其他方法,而不是使用'Where'?

回答

1

您可以使用下面class

/// <summary> 
/// Enables the efficient, dynamic composition of query predicates. 
/// </summary> 
public static class PredicateBuilder 
{ 
    /// <summary> 
    /// Creates a predicate that evaluates to true. 
    /// </summary> 
    public static Expression<Func<T, bool>> True<T>() { return param => true; } 

    /// <summary> 
    /// Creates a predicate that evaluates to false. 
    /// </summary> 
    public static Expression<Func<T, bool>> False<T>() { return param => false; } 

    /// <summary> 
    /// Creates a predicate expression from the specified lambda expression. 
    /// </summary> 
    public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; } 

    /// <summary> 
    /// Combines the first predicate with the second using the logical "and". 
    /// </summary> 
    public static Expression<Func<T, bool>> AND<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) 
    { 
     return first.Compose(second, Expression.AndAlso); 
    } 

    /// <summary> 
    /// Combines the first predicate with the second using the logical "or". 
    /// </summary> 
    public static Expression<Func<T, bool>> OR<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) 
    { 
     return first.Compose(second, Expression.OrElse); 
    } 

    /// <summary> 
    /// Negates the predicate. 
    /// </summary> 
    public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression) 
    { 
     var negated = Expression.Not(expression.Body); 
     return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters); 
    } 

    /// <summary> 
    /// Combines the first expression with the second using the specified merge function. 
    /// </summary> 
    static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge) 
    { 
     // zip parameters (map from parameters of second to parameters of first) 
     var map = first.Parameters 
      .Select((f, i) => new { f, s = second.Parameters[i] }) 
      .ToDictionary(p => p.s, p => p.f); 

     // replace parameters in the second lambda expression with the parameters in the first 
     var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); 

     // create a merged lambda expression with parameters from the first expression 
     return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters); 
    } 

    class ParameterRebinder : ExpressionVisitor 
    { 
     readonly Dictionary<ParameterExpression, ParameterExpression> map; 

     ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map) 
     { 
      this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>(); 
     } 

     public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp) 
     { 
      return new ParameterRebinder(map).Visit(exp); 
     } 

     protected override Expression VisitParameter(ParameterExpression p) 
     { 
      ParameterExpression replacement; 

      if (map.TryGetValue(p, out replacement)) 
      { 
       p = replacement; 
      } 

      return base.VisitParameter(p); 
     } 
    } 
} 

你可以用它为你的情况:

var predicate = PredicateBuilder.False<PropertyPosts>(); 
//predicate = predicate.OR(x=>x.SomeProperties == someValues); 
//predicate = predicate.AND(x=>x.SomeOtherProperties == someOtherValues); 

if (item.ApartmentCondo == 1) 
{ 
    predicate = predicate.OR(x => x.name == item.name && 
         x.PropertyType == PropertyType.ApartmentCondo); 
} 
if (item.House == 1) 
{ 
    predicate = predicate.OR(x => x.name == item.name && 
         x.PropertyType == PropertyType.House); 
} 

List<PropertyPosts> posts2 = posts.Where(predicate).ToList(); 
+0

这对EF Core不起作用。你知道为什么吗?? – snekkke

+0

对不起@snekkke,我不知道EF核心。 – Masoud

+0

对不起。我的错误,它适用于EF核心。我以错误的方式使用它。我改变了错误和真实方法的名称,因为@迈克尔罗森达尔说有点混乱。 – snekkke

1

移动在哪里里面你if条件:

posts = posts.Where(x => x.name == item.name && 
        ( (item.ApartmentCondo == 1 && x.PropertyType == PropertyType.ApartmentCondo) 
        || (item.House == 1   && x.PropertyType == PropertyType.House)); 
1

尝试。

posts= posts.Where(x => x.name == item.name && 
        (
        (item.ApartmentCondo == 1 && x.PropertyType == PropertyType.ApartmentCondo) || 
        (item.House == 1 && x.PropertyType == PropertyType.House)) 
        ); 
+0

你可以请问如何在问题中展示如何动态构建它? –

0

很抱歉给出了新的答案,但是我的评论太少了。

正确/错误的作品,但有点混乱,也产生了多个起点。我修改了代码并删除了True <T>和False <T>方法,并为创建新的过载方法<T>。

public static class PredicateBuilder 
{ 
    public const bool TreatEntireExpressionAsAnd = true; 
    public const bool TreatEntireExpressionAsOr = false; 

    public static Expression<Func<T, bool>> Create<T>(bool value = TreatEntireExpressionAsAnd) { return param => value; } 
    ...