My Question is, Why no SQL for NHibernate 3 Query?

Anyone have answer about my this question: Why is no SQL being generated when I run my Nhibernate 3 query?

    public IQueryable<Chapter> FindAllChapters()
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            var chapters = session.QueryOver<Chapter>().List();

            return chapters.AsQueryable();
        }
    }
If I run the query below I can see that the SQL that gets created.
    public IQueryable<Chapter> FindAllChapters()
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            var resultDTOs = session.CreateSQLQuery("SELECT Title FROM Chapter")
                    .AddScalar("Title", NHibernateUtil.String)
                    .List();

            // Convert resultDTOs into IQueryable<Chapter>
        }
    }
 
Answer Is:
 
Linq to NHibernate (like Linq to entities) uses delayed execution. You are returning IQueryable<Chapter> which means that you might add further filtering before using the data, so no query is executed.
If you called .ToList() or .List() (i forget which is in the API), then it would actually produce data and execute the query.
In other words, right now you have an unexecuted query.
For more info, google "delayed execution linq" for articles like this
 

0 comments: