Chin
Chin

Reputation: 12712

Searching on multiple columns in NHibernate

How would one do the following in NHibernate?

    SELECT  ContactName, ContactTel1, ContactTel2, ContactTel3
    FROM tb_Contact
    WHERE (ContactTel1 LIKE '%6440%') OR
         (ContactTel2 LIKE '%6440%') OR
         (ContactTel3 LIKE '%6440%')

This is what I have, but can't figure out how to do the same with multiple columns.

  all = session.CreateCriteria(typeof(Contact)).Add(Expression.Like(pField,  "6440", MatchMode.Anywhere))
                 .List<Contact>();

Any pointers much appreciated.

Upvotes: 2

Views: 1277

Answers (3)

Juvs
Juvs

Reputation: 96

You miss the MatchMode...

all = session.CreateCriteria (typeof(Contact))
                .Add (
                      Restrictions.Disjunction().Add (Restrictions.Like ("Tel1", "6440", MatchMode.Anywhere)
                                                .Add (Restrictions.Like ("Tel2", "6440", MatchMode.Anywhere)
                                                .Add (Restrictions.Like ("Tel3", "6440", MatchMode.Anywhere)
                     );

Upvotes: 1

Sly
Sly

Reputation: 15247

session.CreateCriteria (typeof(Contract))
                .Add (
                      Restrictions.Like ("Tel1", "6440")||
                      Restrictions.Like ("Tel2", "6440")||
                      Restrictions.Like ("Tel3", "6440")
                     );

Upvotes: 1

Frederik Gheysels
Frederik Gheysels

Reputation: 56984

Take a look at the Disjunction expression.

all = session.CreateCriteria (typeof(Contract))
                .Add (
                      Restrictions.Disjunction().Add (Restrictions.Like ("Tel1", "6440")
                                                .Add (Restrictions.Like ("Tel2", "6440")
                                                .Add (Restrictions.Like ("Tel3", "6440")
                     );

Upvotes: 4

Related Questions