Print Page | Close Window

Multiple Where clause

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3264
Printed Date: 16-Apr-2026 at 2:00pm


Topic: Multiple Where clause
Posted By: k_cire0426
Subject: Multiple Where clause
Date Posted: 13-Feb-2012 at 9:14pm
Hello,

How do I convert this SQL Where Expression to CompositePredicateDescription?

WHERE (AccountID = '123' AND Group = 'Group1') 
OR AccountID = '1234'

thanks



Replies:
Posted By: DenisK
Date Posted: 14-Feb-2012 at 6:42pm
Hi k_cire0426,

Here's an example.

      var pd1 = new PredicateDescription("AccountID", FilterOperator.IsEqualTo, "123");
      var pd2 = new PredicateDescription("Group", FilterOperator.IsEqualTo, "Group1");
      var pd3 = new PredicateDescription("AccountID ", FilterOperator.IsEqualTo, "1234");
      
      var pd = (pd1.And(pd2)).Or(pd3);


Posted By: k_cire0426
Date Posted: 15-Feb-2012 at 9:34pm
hello denisK, :)

Your sample is working, however it seems not working when combining each predicatedescription in a for loop one by one. I have all my PredicateDescription in the ArrayList..

thanks...


Posted By: DenisK
Date Posted: 16-Feb-2012 at 11:01am
Would you be able to post a code snippet?


Posted By: k_cire0426
Date Posted: 16-Feb-2012 at 4:44pm
hello denisK,

Here is the sample code. 

The code below assuming that we have 3 predicates and the 1 and 2 predicate will be in group.

            CompositePredicateDescription compPredicates = null;
            IPredicateDescription predicate = null;
 
            for (int arrCount = 0; arrCount <= arrPredicates.Count; arrCount++)
            {
                if (predicate != null)
                {
                    if (compPredicates == null)
                    {
                        compPredicates = predicate.And((IPredicateDescription)arrPredicates[arrCount]);
                        //tried this as well
                        //compPredicates = (predicate.And((IPredicateDescription)arrPredicates[arrCount]));
                    }
                    else
                    {
			
			//here is the part where i add the parenthesis, but i tried the above as well...
                        compPredicates = (compPredicates).Or((IPredicateDescription)arrPredicates[arrCount]);
                    }                 }                 else                 {                     predicate = (IPredicateDescription)arrPredicates[arrCount];                 }             }


         


Posted By: DenisK
Date Posted: 16-Feb-2012 at 7:24pm
The following works when I tested it.

[TestMethod]
    public void DynamicQueryCombiningPredicates() {
      //T11526
      var pd1 = new PredicateDescription("UnitPrice", FilterOperator.IsGreaterThanOrEqualTo, 24);
      var pd2 = new PredicateDescription("Discontinued", FilterOperator.IsEqualTo, true);
      var pd3 = new PredicateDescription("Category.Name", FilterOperator.StartsWith, "P");
      var arrPredicates = new ArrayList { pd1, pd2, pd3 };
      
      CompositePredicateDescription compPredicates = null;
      IPredicateDescription predicate = null;

      for (int arrCount = 0; arrCount < arrPredicates.Count; arrCount++) {
        if (predicate != null) {
          if (compPredicates == null) {
            compPredicates = predicate.And((IPredicateDescription)arrPredicates[arrCount]);
            //tried this as well
            //compPredicates = (predicate.And((IPredicateDescription)arrPredicates[arrCount]));
          } else {

            //here is the part where i add the parenthesis, but i tried the above as well...
            compPredicates = (compPredicates).Or((IPredicateDescription)arrPredicates[arrCount]);
          }
        } else {
          predicate = (IPredicateDescription)arrPredicates[arrCount];
        }
      }

      var r1 = _em1.Products.Where(p => (p.UnitPrice >= 24 && p.Discontinued) || p.Category.Name.StartsWith("P")).ToList();
      var r2 = _em1.Products.Where(compPredicates).ToList();
      Assert.IsTrue(r1.Count == r2.Count);
    }

I had to change

arrCount <= arrPredicates.Count

to 

arrCount < arrPredicates.Count 

or else it throws index out of range exception.


Posted By: k_cire0426
Date Posted: 17-Feb-2012 at 12:56am
i wonder how it works in your end...I am checking the script on profiler and it definitely not grouping.. i'll find what's causing this... 



Print Page | Close Window