I am tying to rewrite a module which interacts with NHibernate. The buisness logic becomes more sofisticated and we decided to change our linq queries to Nhibernate's Criteria. Here is the old code:
nhSession.Query<User>().Where(
u => u.Roles.Contains(query.Role)
)
And the new code:
var criteria = nhSession.CreateCriteria<User>("user");
criteria.Add(/* contains? */);
And mapping:
<class name="User" table="users">
<id name="Id" column="id">
<generator class="hilo">
<param name="table">hilo</param>
<param name="column">user_id</param>
<param name="max_lo">10</param>
</generator>
</id>
<property name="Password" column="password" />
<bag name="Roles" table="user_roles">
<key column="user_id" />
<element column="role" />
</bag>
</class>
Where Roles is an enum.
How to make the query with Criteria behave like the Linq query?
var criteria = nhSession.CreateCriteria<User>("user");
var roleCriteria = criteria.CreateCriteria("Roles","roles");
roleCriteria.Add(Expression.Eq("role",Role.YourRole);