Nested Predicates question

Posts   
 
    
Posts: 18
Joined: 14-Apr-2005
# Posted on: 26-Apr-2005 17:22:23   

Hello. I'm having trouble coming up with the right predicates expression for my Where clause. I want to emulate this query (notice the parens)....

SELECT * FROM News WHERE ((PublishingStatusId = 1) AND (PendingNewsId is null) OR (PublishingStatusId = 2)) AND NewsDate = 1/1/2005

I want the "NewsDate" Section to not be part of the AND or OR. this is my code which is not working right.


            
            // published and no pending
            expPub.Add(PredicateFactory.CompareValue(NewsFieldIndex.PublishingStatusId, ComparisonOperator.Equal, (int) PublishingStatus.Published));
            expPub.AddWithAnd(PredicateFactory.CompareNull(NewsFieldIndex.PendingNewsId));
            bucket.PredicateExpression.Add(expPub);

            // all pending
            expPend.Add(PredicateFactory.CompareValue(NewsFieldIndex.PublishingStatusId, ComparisonOperator.Equal, (int) PublishingStatus.Working));
            bucket.PredicateExpression.AddWithOr(expPend);

            // by date
            expDate.Add(PredicateFactory.CompareValue(NewsFieldIndex.NewsDate, ComparisonOperator.Equal, newsDate.Date));           
            bucket.PredicateExpression.Add(expDate);
            
            adapter.FetchEntityCollection(ec, bucket, 0, new SortExpression(SortClauseFactory.Create(NewsFieldIndex.UpdatedDate, SortOperator.Descending)));

this is generating the following SQL (notice the parens):

 SELECT * FROM [News] 
WHERE (([PublishingStatusId] = 1 And [PendingNewsId] IS NULL) Or ([PublishingStatusId] = 2)
     And ( [NewsDate] = 'Apr 26 2005 12:00:00:000AM')) 

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Apr-2005 18:03:03   

As I see it, you add the OR predicate to the wrong expression.

If you instead of


expPend.Add(PredicateFactory.CompareValue(NewsFieldIndex.PublishingStatusId, ComparisonOperator.Equal, (int) PublishingStatus.Working));

do:


expPub.Add(PredicateFactory.CompareValue(NewsFieldIndex.PublishingStatusId, ComparisonOperator.Equal, (int) PublishingStatus.Working));

it should work

Frans Bouma | Lead developer LLBLGen Pro
Posts: 18
Joined: 14-Apr-2005
# Posted on: 26-Apr-2005 18:19:52   

ahh yes, i got it. thank you simple_smile