Pregunta

How can we add multiple Or and And Operations in where clause in Predicate Description of ideablade. ex.

 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);

As Above I have tried to adding Or operator in CompositePredicateDescription, but it is not working

I want to run following query -

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)

How can I run above query using ideablade.

¿Fue útil?

Solución

You should be able to use CompositePredicateDescription in this situation without problems. How is it not working?

Here's an example:

  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);

The code above will generate the WHERE statement below (from SQL Profiler) which has the same pattern as the WHERE statement in your query:

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

If you haven't done so already, you might want to take a look at the DevForce Resource Center article on dynamic WHERE clauses.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top