views:

144

answers:

3

I am creating a method that can create filter understood by NHibernate (by filter i mean a set of ICriteria object for example) from my abstract filter object.

public static IEnumerable<ICriterion> ToNhCriteria(this MyCriteria criteria)
{
   // T4 generated function
   // lots of result.Add(Expression.Or(Expression.Eq(),Expression.Eq)) expression trees - hard to generate
   // Is there a way to generate HQL/Linq query here istead?
}

then i want to do something like

session.CreateCriteria<Entity>().Add(myCriteria.ToNhCriteria())

to filter entities. The problem is that using Expression. methods (Expression.Or etc) is quite tedious (the method is generated and i have multiple or statements that have to be joined into an expression somehow). Is there a way to avoid using Expression.Or() and create ICrietrion / ICriteria using LINQ or HQL?

A: 

No that is not possible. Why don't you use linq instead of criteria?

Paco
A: 

Linq is not the best solution unless you want to do filtering on collection-side not on datbase-side using WHERE clauses. Ayende suggests that ICriteria API is well suited for dynamic filter creation, the problem i had with multiple ORs has been tackled by using Restrictions.Disjunction()... that simplified a lot At the time I asked the question I just didn't realize such things exist in NHibernate :)

adrin
+1  A: 

Hey, did you check out this question? It shows going from Linq to NHibernate to a MultiCriteria (and on the way transforms a linq query to an ICriteria)

ZeroBugBounce