tags:

views:

155

answers:

3

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.

+3  A: 

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")
                     );
Frederik Gheysels
That seemed to do it. Thanks.
Chin
+1  A: 
session.CreateCriteria (typeof(Contract))
                .Add (
                      Restrictions.Like ("Tel1", "6440")||
                      Restrictions.Like ("Tel2", "6440")||
                      Restrictions.Like ("Tel3", "6440")
                     );
Sly
I don't think that will work ?
Frederik Gheysels
Have you tried?
Sly
Indeed, that works as well. Never knew it. thx.
Frederik Gheysels
you are welcome :)
Sly
+1  A: 

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)
                     );
Juvs
Jesus, I don't think it is necessary to give the complete detailed code.I think the Disjunction part of my post is the most important one to the topicstarter; that was the missing part. He can write the remaining part of the solution himself, can't he ? We're not monkeys, are we ...
Frederik Gheysels