开发者

Using NHibernate.Linq and getting 2 queries for a simple select, why?

开发者 https://www.devze.com 2023-01-10 21:35 出处:网络
so here\'s the code with irrelevant bits left out: public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> filter

so here's the code with irrelevant bits left out:

public IEnumerable<T> GetByQuery(Expression<Func<T, bool>> filter
{
    try 
    {
        return Session.Linq<T>().Where(filter);
     }
    catch(Exception ex)
    {
        // custom exception handling here
    }
    finally
    {
        CloseSession();
    }
    return null;
}

and an example of it being called looks like this:

IEnumerabl<ClientReport> clientReports = 
clientReportRepository.GetByQuery(item => item.ClientId = id);

So as you can see, nothing fancy and being called in this way, we're hitting one table in the datab开发者_高级运维ase with no relationships to any other tables. But when I have show_sql = true in the configuration, It's displaying 2 of the same query. Any ideas? Thanks


clientReports will probably execute the query every time you enumerate it (or get the Count(), for example).

To avoid that, use .ToList() in the assignment.

0

精彩评论

暂无评论...
验证码 换一张
取 消