New Posts New Posts RSS Feed: PredicateBuilder with ands / ors
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

PredicateBuilder with ands / ors

 Post Reply Post Reply
BillG View Drop Down
DevForce MVP
DevForce MVP

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: PredicateBuilder with ands / ors
    Posted: 28-Apr-2014 at 1:41pm
I know how to use Predicate Descriptions with Ands.

I have the following code

public IEnumerable<MembersAndJobHistory> GetMembersForBatchByEmployer(Batch batch)
           IPredicateDescription compositePred;
           PredicateDescription p1 = null, p2 = null, p3 = null, p4 = null, p5 = null, p6 = null;
           p1 = PredicateBuilder.Make(typeof(MembersAndJobHistory), "Status"FilterOperator.IsEqualTo, "A");
           p2 = PredicateBuilder.Make(typeof (MembersAndJobHistory), "EmployerNo"FilterOperator.IsEqualTo,
           p3 = PredicateBuilder.Make(typeof(MembersAndJobHistory), "PayVia"FilterOperator.IsEqualTo, "E");
           if (!CommonControl.IsControlTrue("NoMonthlyDues"))
               p4 = PredicateBuilder.Make(typeof(MembersAndJobHistory), "Dues"FilterOperator.IsEqualTo, 0);
           if (batch.BillYN != null)
               p5 = PredicateBuilder.Make(typeof(MembersAndJobHistory), "BillYN"FilterOperator.IsEqualTo, batch.BillYN);             
           p6 = PredicateBuilder.Make(typeof(MembersAndJobHistory), "TSStart"FilterOperator.IsLessThanOrEqualTo, batch.PayPeriodEnd);
           compositePred = CombinePreds(p1, p2, p3, p4, p5, p6);
           var query = Manager.MembersAndJobHistories
               .OrderBy(m => m.SocSecNo);
           var results = query.Execute();
           return results;

public IPredicateDescription CombinePreds(params PredicateDescription[] preds)     {         var nonNullPreds = preds.Where(pd => pd != null).ToArray();         var result = PredicateBuilder.And(nonNullPreds);         return result;     }

Now i need to add two more predicates to the query. Basically i want to And an Or. For example, and where
 TSFinish >= batch.PayPeriod or TSFinish is null

How do i fix my code to handle these 2 new predicates?

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down