2013-03-20 85 views
1

如何添加多个And和Or在Where子句中如何在Predicate Descriptionabar的where子句中添加多个Or和And Operations。 ex。如何使用Ideablade谓词描述

List <PredicateDescription> pdList = new List<PredicateDescription>(); 

Dictionary< int, List< PredicateDescription>> pdDateList = new Dictionary< int, List< PredicateDescription>>(); 

var pds = pdList.ToArray(); 

if (((pds.Length > 0) || (pdDateList.Count > 0))) 
{ 

    CompositePredicateDescription predicate = null; 
    if (pds.Length > 0) 
    { 
       predicate = PredicateBuilder.And(pds); 
    }      
    List<PredicateDescription> AndList = null;      
    CompositePredicateDescription compositePredicateDescription = null; 
    for (int i = 0; i < pdDateList.Count; i++) 
    { 
      List<PredicateDescription> pdlist1 = pdDateList[i]; 
      AndList = new List<PredicateDescription>(); 
      foreach (PredicateDescription pdesc in pdlist1) 
      { 
        AndList.Add(pdesc); 
      } 
      if (AndList.Count > 0) 
      { 
       if (predicate != null) 
       { 
         if (AndList.Count == 2) 
         { 
          predicate.Or(AndList[0].And(AndList[1])); 
         } 
         else if (AndList.Count == 1) 
         { 
          predicate.Or(AndList[0]); 
         } 
       } 
      } 
    } 
} 

if (predicate == null) 

{ 

predicate = compositePredicateDescription; 

} 

       var filterQuery = query.Where(predicate);      
       data = Globals.sLDManager.ExecuteQuery(filterQuery); 

正如上面我曾试图在CompositePredicateDescription添加或运营商,但它不工作

我想下面的查询运行 -

Select * from Tabel1 where t.field1=1 and t.field2=1 and t.field3=1 Or (t.field4=1 and t.field5=1) Or (t.field6=1 and t.field7=1)

如何运行上面的查询使用ideablade。

回答

2

在这种情况下,您应该可以使用CompositePredicateDescription,而不会出现任何问题。它怎么不起作用?

下面是一个例子:

PredicateDescription p1 = PredicateBuilder.Make(typeof(Product), "UnitPrice", FilterOperator.IsGreaterThanOrEqualTo, 24); 
    PredicateDescription p2 = PredicateBuilder.Make(typeof(Product), "Discontinued", FilterOperator.IsEqualTo, true); 

    CompositePredicateDescription p3 = p1.And(p2); 

    PredicateDescription p4 = PredicateBuilder.Make(typeof(Product), "UnitsInStock", FilterOperator.IsGreaterThanOrEqualTo, 1); 
    PredicateDescription p5 = PredicateBuilder.Make(typeof(Product), "UnitsOnOrder", FilterOperator.IsGreaterThanOrEqualTo, 1); 

    CompositePredicateDescription p6 = p1.And(p2); 

    CompositePredicateDescription p7 = p3.Or(p6); 

    var query = mgr.Products.Where(p7); 

上面的代码会生成以下WHERE语句(从SQL事件探查器),它具有相同的图案,在查询的WHERE语句:

WHERE (([Extent1].[UnitPrice] >= cast(24 as decimal(18))) AND (1 = [Extent1].[Discontinued])) OR (([Extent1].[UnitPrice] >= cast(24 as decimal(18))) AND (1 = [Extent1].[Discontinued])) 

如果你还没有这样做,你可能想看看动态WHERE子句中的DevForce Resource Center文章。

+0

感谢您的回复..我也执行了上述相同的代码。这个对我有用。 – user1782872 2013-03-22 12:30:17