EntityCollection with a LIKE on two fields

Posts   
 
    
BigVee
User
Posts: 6
Joined: 23-Mar-2006
# Posted on: 23-Mar-2006 00:19:03   

Howdy,

I've got a search function that searches through contacts finding if the input string is in either the firstname OR lastname fields... but I don't quite know how to go about this using the generated code.

This is what I have so far:


public static EntityCollection GetContactCollectionFromString(string strSearch)
        {
            EntityCollection toReturn = new EntityCollection(new ContactEntityFactory());


            char[] delimiter = ", ".ToCharArray();
            String[] strSearchParts = strSearch.Split(delimiter);

            IPredicateExpression filter = new PredicateExpression();
            for (int i = 0; i < strSearchParts.Length; i++)
            {
                filter.Add(PredicateFactory.Like(DALProg.ContactFieldIndex.LastName, "%" + strSearchParts[i].Trim() + "%"));
                filter.Add(PredicateFactory.Like(DALProg.ContactFieldIndex.FirstName, "%" + strSearchParts[i].Trim() + "%"));
            }

            IRelationPredicateBucket filterbucket = new RelationPredicateBucket();
            filterbucket.PredicateExpression.Add(filter);
            
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(toReturn, filterbucket);
            }

            return toReturn;
        }

But that code means that the search string must be found in both firstname and lastname.

Is there any way I can do it so that it is found in the firstnam OR lastname?

If not, is there a way I can merge two collections?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 23-Mar-2006 01:45:58   

Try filter.AddWithOr(PredicateFactory.Like(DALProg.ContactFieldIndex.FirstName, "%" + strSearchParts[i].Trim() + "%"));

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 23-Mar-2006 02:01:06   

With 2005 you can do something like this:

VB


Dim find as string = "%" & txtSearch.Text.Trim() & "%"
Dim filter As PredicateExpression = _
(ContactFields.FirstName Mod find) Or _
(ContactFields.LastName Mod find)

bucket.PredicateExpression.Add(filter)

C#


string find = "%" + txtSearch.Text.Trim() + "%";

PredicateExpression filter = (ContactFields.FirstName % find) | (ContactFields.LastName % find);

bucket.PredicateExpression.Add(filter);

BigVee
User
Posts: 6
Joined: 23-Mar-2006
# Posted on: 23-Mar-2006 02:58:32   

Thank you bclubb & Jim.

            char[] delimiter = ", ".ToCharArray();
            String[] strNameParts = strName.Split(delimiter);

            IRelationPredicateBucket filterbucket = new RelationPredicateBucket();
            for (int i = 0; i < strNameParts.Length; i++)
            {
                string find = "%" + strNameParts[i].Trim() + "%";
                PredicateExpression filter = (ContactFields.FirstName % find) | (ContactFields.LastName % find);
                filterbucket.PredicateExpression.Add(filter);
            }

Is the code i decided to go with.

Say I had two contacts named Peter Griffin and Stewie Griffin and I inputted a search string of p grif

Using that code I would get back only Peter Griffin because I'm guessing it structures the query something like:

WHERE (FirstName LIKE %p% OR LastName LIKE %p%) AND (FirstName LIKE %grif% OR LastName LIKE %grif%)

Where as the other way (using filter.AddWithOr) I would get back both Peter and Stewie because I'm guessing it structures the query something like:

WHERE FirstName LIKE %p% OR LastName LIKE %p% OR FirstName LIKE %grif% OR LastName LIKE %grif%

Thanks again, Charles