2

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?

adrin
  • 3,738
  • 8
  • 40
  • 60
  • 2
    Are you using T4 at design time or runtime to generate ICriteria? If at design time, what is stopping you from using T4 to generate HQL (or Linq) instead? – Michael Maddox Apr 27 '10 at 17:31
  • Yes I am using T4 in design time and as you suggested i could use HQL or Linq instead of ICriteria. The problem was caused by my lack of knowledge rather than by framework limitations. Currently I am using ICriteria API though as it seems to be most readable for someone reading T4 and it works properly now. – adrin Apr 28 '10 at 07:16

3 Answers3

1

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)

Community
  • 1
  • 1
ZeroBugBounce
  • 3,652
  • 3
  • 31
  • 40
0

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

Paco
  • 8,335
  • 3
  • 30
  • 41
0

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
  • 3,738
  • 8
  • 40
  • 60