tags:

views:

59

answers:

2

I think I know what the answer to this question is probably going to be, but I thought I'd go ahead and ask it anyway.

It appears that within NHibernate if I do something like this:

IList<Customer> customers = Session.CreateCriteria(typeof(Customer))
                                .Add(Restrictions.Eq("Name", "Steve")
                                .List<Customer>();

And I want to then delete that list of customers. From what I can tell the only way to do it is like this:

foreach(var customer in customers)
{
    Session.Delete(customer);
}

But what I'm wondering is if there's any way I can just go:

Session.Delete(customers);

And delete the entire collection with a single call?

A: 

I would have thought that the latter is valid.

Preet Sangha
unfortunately it doesn't appear to be
lomaxx
i'll check how we do it.
Preet Sangha
+3  A: 

Not with Criteria, but it's easy to do with HQL:

session.CreateQuery("delete Customer customer where customer in (:customers)")
       .SetParameterList("customers", customers.ToArray())
       .ExecuteUpdate();

But you don't need to load them. You can also do it in one shot:

session.CreateQuery("delete Customer where Name = 'Steve'")
       .ExecuteUpdate();
Diego Mijelshon