New Posts New Posts RSS Feed: PredicateDescription and Composite
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

PredicateDescription and Composite

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

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Topic: PredicateDescription and Composite
    Posted: 06-Mar-2014 at 7:01am
I have a form with 6 fields on it which are search criteria fields. They are SSN, EmployeeNo, CarcNo, LastName, FirstName, Status and office. When the user clicks Find, I check to see which ones are filled in and I build a MemberQO query object. I then pass the MemberQO to my Repository method GetMembers() where I do the following.

       public IEnumerable<MembersList> GetMembers(MemberQO memberQO)
        {
            CompositePredicateDescription cpd;
            PredicateDescription p1, p2, p3, p4, p5, p6 = null;

            if (memberQO.SocSecNo != string.Empty)
            {
               p1 = PredicateBuilder.Make(typeof (Member), "SocSecNo", FilterOperator.IsEqualTo, memberQO.SocSecNo);
            }
            if (memberQO.CardNo != string.Empty)
            {
               p2 = PredicateBuilder.Make(typeof (Member), "CardNo", FilterOperator.IsEqualTo,
                    memberQO.CardNo);
              
            }
            if (memberQO.EmployeeNo != string.Empty)
            {
               p3 = PredicateBuilder.Make(typeof(Member), "EmployeeNumber", FilterOperator.IsEqualTo,
                    memberQO.EmployeeNo);
            }
            if (memberQO.LastName != string.Empty)
            {
                 p3 = PredicateBuilder.Make(typeof (Member), "LastName", FilterOperator.IsEqualTo,
                    memberQO.LastName);

            }
            if (memberQO.FirstName != string.Empty)
            {
               p4 = PredicateBuilder.Make(typeof(Member), "FirstName", FilterOperator.IsEqualTo,
                    memberQO.FirstName);
            }
            if (memberQO.Office != string.Empty)
            {
                 p5 = PredicateBuilder.Make(typeof(Member), "Office", FilterOperator.IsEqualTo,
                    memberQO.Office);
            }
            if (memberQO.Status != string.Empty)
            {
               p6 = PredicateBuilder.Make(typeof(Member), "Status", FilterOperator.IsEqualTo,
                    memberQO.Status);
            }

            var query = Manager.MembersLists
               .OrderBy(m => m.SocSecNo);

            var results = query.Execute();
            return results;
        }

    Now what is the best way to combine those into a CompositePredicateDescription, None, some, or all of the search fields could be used.
Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 07-Mar-2014 at 12:41pm
any help on this one?
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 07-Mar-2014 at 6:48pm
Hi Bill,

This should take you in the right directions.

http://drc.ideablade.com/devforce-2012/bin/view/Documentation/predicatebuilder-methods
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 11-Mar-2014 at 9:36am
Hi Bill,

This page that shows how to create a completely dynamic DevForce queries might also interest you.

Back to Top
BillG View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 05-Dec-2007
Location: Monroe, MI
Posts: 233
Post Options Post Options   Quote BillG Quote  Post ReplyReply Direct Link To This Post Posted: 12-Mar-2014 at 12:02pm
thanks to Jay from IdeaBlade, I have a solution to my problem which i wish to share in case others come across this same situation.

This code allows me to pass a Query Object to my Repository GetMembers() method and then build a predicateDescription for each property of the QueryObject that is not null. i then pass the PredicateDescription objects to the CombinePreds as an arrary and the CombinePreds picks out the non nulls ones and combines them. It then passes the combined one back to my Get method which then is used in the .Where clause of the query.

Thans again to Jay for your help. As I have always said IdeaBlade has a great support team for the customers.

public IPredicateDescription CombinePreds(params PredicateDescription[] preds)
    {
        var nonNullPreds = preds.Where(pd => pd != null).ToArray();
        var result = PredicateBuilder.And(nonNullPreds);
        return result;
    }
     
    
    public IEnumerable<MembersList> GetMembers(MemberQO memberQO)
    {
        IPredicateDescription compositePred;
        PredicateDescription p1 = null, p2 = null, p3 = null, p4 = null, p5 = null, p6 = null;
 
        if (memberQO.SocSecNo != null)
        {
            p1 = PredicateBuilder.Make(typeof (MembersList), "SocSecNo"FilterOperator.IsEqualTo,  memberQO.SocSecNo);
        }
        if (memberQO.CardNo != null)
        {
            p2 = PredicateBuilder.Make(typeof (MembersList), "CardNo"FilterOperator.IsEqualTo,
                memberQO.CardNo);
           
        }
        if (memberQO.EmployeeNo != null)
        {
            p3 = PredicateBuilder.Make(typeof(MembersList), "EmployeeNumber"FilterOperator.IsEqualTo,
                memberQO.EmployeeNo);
        }
        if (memberQO.LastName != null)
        {
             p3 = PredicateBuilder.Make(typeof (MembersList), "LastName"FilterOperator.StartsWith,
                memberQO.LastName);
 
        }
        if (memberQO.FirstName != null)
        {
            p4 = PredicateBuilder.Make(typeof(MembersList), "FirstName"FilterOperator.StartsWith,
                memberQO.FirstName);
        }
        if (memberQO.Office != null)
        {
             p5 = PredicateBuilder.Make(typeof(MembersList), "Office"FilterOperator.IsEqualTo,
                memberQO.Office);
        }
        if (memberQO.Status != null)
        {
            p6 = PredicateBuilder.Make(typeof(MembersList), "Status"FilterOperator.IsEqualTo,
                memberQO.Status);
        }
 
        compositePred = CombinePreds(p1, p2, p3, p4, p5, p6);
 
        var query = Manager.MembersLists
            .Where(compositePred)
            .OrderBy(m => m.SocSecNo);
 
        var results = query.Execute();
        return results;
    }

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down