views:

168

answers:

2

Is it possible to get list after Session.Close as below?

var bundles = session.Linq<Bundle>();
session.Close();
var bs = bundles.ToList();

I get error, is there a different syntax for that?

+3  A: 

You can't. Session is used for holding a connection to the data base. After you close the session, you cant get access to DB

Sly
But I wanna use IQueryable object, outside Nhibernate DataAccess Layer.
NetSide
You either return ToList from your DataAccess layer. Or you don't close the session and transaction.
Aaron Fischer
If you are working on web project you can use session per requrest strategy.http://frickinsweet.com/ryanlanciaux.com/post/NHibernate-Session-Per-Request.aspx
Sly
What happens if we dont close session? Is it close itself when method ends?
NetSide
Not, it won't be closed until garbage collection
Sly
+1  A: 

It looks like you're going about this the wrong way. You need to use the UnitOfWork pattern. INSIDE you're unit of work is where you do things with your IQueriable. You can't just pass an IQueriable around because of it's dependence on an ISession. Perhaps your syntax will look like this:

public void DoSomethingWithData()
{
    IList<Bundles> qbundles;
    using (var uof = new UnitOfWork())
    {
         IQueriable<Bundle> bundles = uof.Repository<Bundle>().Query();
         // just a litte example... replace with whatever
         qbundles = bundles.Where(b => b.Qty == 5).ToList()
    }
    ....
}

Here are some links that might get you started with this pattern:

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx

http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx

zowens