【问题标题】:How to add multiple And and Or in where clause using Ideablade Predicate Description如何使用 Ideablade 谓词描述在 where 子句中添加多个 And 和 Or
【发布时间】:2013-03-09 03:04:02
【问题描述】:

我们如何在ideablade的谓词描述的where子句中添加多个Or和And操作。 例如。

 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 运行上述查询。

【问题讨论】:

    标签: .net c#-4.0 predicatebuilder devforce


    【解决方案1】:

    在这种情况下,您应该可以毫无问题地使用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 Profiler),它与查询中的 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]))
    

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

    【讨论】:

    • 感谢回复.. 我也实现了与上面相同的代码。它对我有用。
    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多