Reputation: 12712
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
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
Reputation: 15247
session.CreateCriteria (typeof(Contract))
.Add (
Restrictions.Like ("Tel1", "6440")||
Restrictions.Like ("Tel2", "6440")||
Restrictions.Like ("Tel3", "6440")
);
Upvotes: 1
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