I sent the above answer to Mark's original discussion SO. Good to see
that others find it interesting too.

I earlier used LinqToNHibernate for some applications, where I really
liked that it was possible to keep all data access code type safe
without any magic HQL/SQL strings. I was thrilled to see that NH3.0
brought even better LINQ support, and that it was built into the
NHibernate assembly itself. Complex deletes based on filters rather
than single object deletes seems to be the only place left where I
have to use HQL strings. So I tried find out if this had changed in
the new version, and found Mark's discussion in that process. I
decided to find a solution looking at the NH code and came up with the
above code, which I thought was pretty close.

My goal was to take my IQueryable and convert it to a HQL string, and
use that to call Delete. This was simply because I thought that
somewhere down the line my IQueryable would be converted to an HQL
string before being sent to the database. I'm not the most experienced
in NHibernate, so I could be wrong, and it seems I am according to
Ayende's answer. So probably I chose the wrong path and reached a dead
end, even though it seems close to the finish line.

But nevertheless I think it could be a very useful feature to have an
overload of Delete, that took a type safe query of some kind rather
than HQL. I suggested an overload using IQueryable like this: int
Delete<T>(IQueryable<T> query). But I can see that it could be
anything and may be hard to implement. Gunnar's suggestion of using
Expression<Func<T, bool>> instead also seems fine. José: When you
write "could be achieved with an extension method", I guess mean a
method that converts Expression<Func<T,bool>> to a HQL string and then
calls ISession.Delete(string query)? Do you mean this is already
possible with NHibernate, and can you guide me in the right direction?

Best regards
Hendrik

On 18 Jan., 20:21, Gunnar Liljas <[email protected]> wrote:
> Absolutely! And I prefer it that way :)
>
> /G
>
> 2011/1/18 José F. Romaniello <[email protected]>
>
>
>
> > Yours could be achieved with an extension method, because it is much more
> > specific.
> > The other could be any any query.
>
> > 2011/1/18 Gunnar Liljas <[email protected]>
>
> >> or
>
> >> ISession.Delete<T>(Expression<Func<T,bool>> query)
>
> >> 2011/1/18 Fabio Maulo <[email protected]>
>
> >> I think he need a new overload of ISession.Delete (the LINQ version of
> >>> ISession.Delete(string)).
> >>> Perahaps:
> >>> ISession.Delete(IQueriable)
>
> >>> On Tue, Jan 18, 2011 at 6:10 AM, Ayende Rahien <[email protected]>wrote:
>
> >>>> We need to see the actual code that he uses.
> >>>> In general, an HqlQuery isn't translated to HqlString, it is then
> >>>> being executed.
>
> >>>> On Jan 18, 11:02 am, Mark Kharitonov <[email protected]>
> >>>> wrote:
> >>>> > Dear ladies and sirs.
>
> >>>> > This post is related to this one on SO -
> >>>>http://stackoverflow.com/questions/1327306/does-anyone-know-how-to-tr.
> >>>> ..
>
> >>>> > 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]<nhusers%[email protected]­>
> >>>> .
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/nhusers?hl=en.
>
> >>> --
> >>> Fabio Maulo
>
> >>>  --
> >>> 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]<nhusers%[email protected]­>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/nhusers?hl=en.
>
> >>  --
> >> 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]<nhusers%[email protected]­>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/nhusers?hl=en.
>
> >  --
> > 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]<nhusers%[email protected]­>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.- Skjul tekst i anførselstegn -
>
> - Vis tekst i anførselstegn -

-- 
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