Dear ladies and sirs.

This post is related to this one on SO -
http://stackoverflow.com/questions/1327306/does-anyone-know-how-to-translate-linq-expression-to-nhibernate-hql-statement

In short, how can one use linq to delete entities?

There a responder who claims to be 95% along the way to the solution
using NH3, but he is missing the final step.

BEGIN QUOTE
____________________________________________________________________________________
I had the exact same need: I needed to delete using a LINQ expression,
but NHibernate only supports delete using HQL or SQL. I don't like
this approach, since the rest of the code is completely strongly typed
with LINQ expression, and I then had to relate to table and property
names and manipulating strings.

I am working with NHibernate 3.0 and I came 95% of the way, but I had
to use reflection to call some private/internal methods on the way.
The below gives me an HqlQuery object for the LINQ expression:

NhQueryable<Product> queryable = (from p in session.Query<Product>()
                             where p.ProductId == 1
                             select p) as NhQueryable<Product>;
if (queryable != null)
{
  Expression expression = queryable.Expression;
  NhQueryProvider provider = queryable.Provider as NhQueryProvider;
  MethodInfo prepareQueryMethod =
typeof(NhQueryProvider).GetMethod("PrepareQuery",
BindingFlags.Instance | BindingFlags.NonPublic);
  object[] arguments = new object[] {expression, null, null};
  NhLinqExpression nhLinqExpression =
prepareQueryMethod.Invoke(provider, arguments) as NhLinqExpression;
  ExpressionToHqlTranslationResults translationResults =
nhLinqExpression.ExpressionToHqlTranslationResults;
  HqlQuery hql = translationResults.Statement as HqlQuery;
}

I am stuck on that I am not able to convert the HqlQuery object to a
HQL string. Anyone have any input on this? Or did you solve your
problem in a different way?

Anyway I think it could be a great addition to have an overload of
ISession.Delete, that took an IQueryable or IQuery as parameter. I am
a newbie to NHibernate, but it seems to me it should be a fairly
simple task for someone knowing NHibernate to find some already
existing methods and wire them up to do the job.
____________________________________________________________________________________
END QUOTE

I was wondering if any one on this mailing list can complete the
missing 5%.
Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to