Searching across multiple columns in NHibernate
How do I 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 cannot 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 are greatly appreciated.
+2
Chin
source
to share
3 answers
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")
);
+4
Frederik gheysels
source
to share
session.CreateCriteria (typeof(Contract))
.Add (
Restrictions.Like ("Tel1", "6440")||
Restrictions.Like ("Tel2", "6440")||
Restrictions.Like ("Tel3", "6440")
);
+1
Sly
source
to share
You are missing 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)
);
+1
Juvs
source
to share