NHibernate Criteria - How to filter on combination of properties

后端 未结 2 1638
太阳男子
太阳男子 2020-12-16 08:53

I needed to filter a list of results using the combination of two properties. A plain SQL statement would look like this:

SELECT TOP 10 *
FROM Person
WHERE F         


        
2条回答
  •  渐次进展
    2020-12-16 09:18

    On the pure technical side i don't have an answer, but consider this: since you are only have a single input field for the user to enter the term, you don't know if he is going to enter 'foo bar' or 'bar foo'... so i would recommend this:

    ICriteria criteria = Session.CreateCriteria(typeof(Person));
    criteria.Add(Expression.Like("FirstName",term, MatchMode.Anywhere) || Expression.Like("LastName",term, MatchMode.Anywhere));
    criteria.SetMaxResults(10);
    

提交回复
热议问题