NetSide
NetSide

Reputation: 3890

Nhibernate-Linq: How can I use Iqueryable list after session close?

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?

Upvotes: 1

Views: 1410

Answers (2)

zowens
zowens

Reputation: 1566

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://web.archive.org/web/20090803155753/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

Upvotes: 1

Sly
Sly

Reputation: 15217

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

Upvotes: 2

Related Questions