2016-11-09 53 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; } 
    ...