在NHibernate中搜索多个列

时间:2009-09-21 13:00:16

标签: nhibernate

如何在NHibernate中执行以下操作?

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

这就是我所拥有的,但无法弄清楚如何对多列做同样的事情。

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

任何指针都非常赞赏。

3 个答案:

答案 0 :(得分:4)

看看Disjunction表达式。

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

你错过了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)
                     );