Search concatenated fields

Posts   
 
    
Miles
User
Posts: 2
Joined: 02-Dec-2011
# Posted on: 02-Dec-2011 20:26:08   

I'm extremely new to LLBL Gen and I'm trying to search through a patient table. The normal SQL I would execute is below.

I understand how to do the last two predicates but how do I do the first predicate where I am wanting to search two fields concatenated together?

DECLARE @SearchText VARCHAR(50); SET @SearchText = 'bob jones'

SELECT * FROM Patient WHERE FirstName + ' ' + LastName LIKE '%' + @SearchText + '%' OR FirstName LIKE '%' + @SearchText + '%' OR LastName LIKE '%' + @SearchText + '%'

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Dec-2011 21:50:20   

You could use DBFunctionCall for that. This is an example:

EntityField concatenatedExpr= PatientFields.FisrtName;
concatenatedExpr.ExpressionToApply = new DbFunctionCall("({0} + {1})", new object[] { PatientFields.FirstName, PatientFields.LastName});

var filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(concatenatedExpr % someSearch);
filter.PredicateExpression.AddWithOr(PatientFields.FirstName % someSearch);
filter.PredicateExpression.AddWithOr(PatientFields.LastName % someSearch);
David Elizondo | LLBLGen Support Team
Miles
User
Posts: 2
Joined: 02-Dec-2011
# Posted on: 02-Dec-2011 22:35:36   

Perfect, that did exactly what I wanted it to.

Below is what I finally used:

internal static PatientCollection SearchMyPatients(string searchText)
        {
            searchText = String.Format("%{0}%", searchText);

            EntityField concatenatedExpression = PatientFields.FirstName;
            concatenatedExpression.ExpressionToApply = new DbFunctionCall("{0} + ' ' + {1}", new object[] { PatientFields.FirstName, PatientFields.LastName});

            PredicateExpression predicate = new PredicateExpression();
            predicate.Add(PatientFields.DoctorId == SessionManager.CurrentDoctorId);
            predicate.AddWithAnd(PatientFields.FirstName % searchText | PatientFields.LastName % searchText | concatenatedExpression % searchText);
            SortExpression sort = new SortExpression();
            sort.Add(PatientFields.LastName | SortOperator.Ascending);

            return GetPatients(predicate, sort);
        }