Tuesday, November 4, 2014

How to use ParameterBuilder with BeginGroup and EndGroup

Summary

Use this methods when you want to group search queries.

The query will be formatted to something like this:

Select * from Survey_Customer where SurveyHeaderID = XXX And ( (FirstName=xxx) or (LastName=xxx) or ...))



       TList<SurveyCustomer> scList = new TList<SurveyCustomer>();

            SurveyCustomerParameterBuilder pb = new SurveyCustomerParameterBuilder();
            pb.AppendEquals(SurveyCustomerColumn.SurveyHeaderId, surveyHeaderId.ToString());            
              
            pb.BeginGroup("And");
            pb.Junction = ""; // need this empty space after calling BeginGroup
            pb.AppendLike(SurveyCustomerColumn.Company, "%" + keyword + "%");
            pb.Junction = "or";
            pb.AppendEquals(SurveyCustomerColumn.FirstName, keyword);
            pb.Junction = "or";
            pb.AppendEquals(SurveyCustomerColumn.LastName, keyword);
            pb.Junction = "or";
            pb.AppendEquals(SurveyCustomerColumn.EmailAddress, keyword);
            pb.Junction = "or";
            pb.AppendLike(SurveyCustomerColumn.Comments, "%" + keyword + "%");
            pb.Junction = "or";
            pb.AppendLike(SurveyCustomerColumn.Note1, "%" + keyword + "%");
            pb.Junction = "or";
            pb.AppendLike(SurveyCustomerColumn.Url, "%" + keyword + "%");
            try // this needs to go in a try catch since db is expecting a long and user might type in a 'string'
            {
                long surveyCustomerId = long.Parse(keyword);
                pb.AppendEquals(SurveyCustomerColumn.SurveyCustomerId, surveyCustomerId.ToString());
                pb.Junction = "or";
            }
            catch (Exception ex)
            {
                // not a valid long
            }
            pb.Junction = ""; // need this empty space before calling EndGroup
            pb.EndGroup();

            scList = DataRepository.SurveyCustomerProvider.Find(pb.GetParameters());

No comments:

MS in Computer Science with paid training in USA company