开发者

NHibernate query using Expression<Func, T>> not working

I have a repository interface which defines the following method:

IEnumerable<T> GetMany(Expression<Func<T, bool>> where);

I have implemented two, almost identical repositories which implement this interface. My first repository uses the Entity Framework. It implements the above method as:

private readonly IObjectSet<T> _objectSet;

public IEnumerable<T> GetMany(Expression<Func<T, bool>> whereExpression)
    {
        return _objectSet.Where(whereExpression);
    }

When I run my program I can see that the above method successfully translates 'whereExpression' into SQL and the query only returns entities which match my whereExpression.开发者_如何学Go

My second repository uses NHibernate and implements the method as follows:

private readonly ISession _context;

public IEnumerable<T> GetMany(Expression<Func<T, bool>> whereExpression)
    {
        return _context.Query<T>().Where(whereExpression);
    }

The above doesn't work though. My query returns nothing. It doesn't look to me like any SQL is being generated at all when I use NHibernate. Can anyone explain how I can get this to work using NHibernate? Ultimately I want to be able to pass a where clause to th repository and have NHibernate translate that into SQL.

As an aside point, I read that I need to use Expression> as opposed to just Func when implementing this. I can successfully get this to work using Func, but it seems to load every record from the database and apply the whereExpression afterwards. This means I load far more entities than I actually want. Is my understanding of this correct?

***EDIT TO ORIGINAL POST

This is very strange, but I've just gotten this to work by changing my whereExpression. I had been requested that the query return all entities whose ID was an even number:

var results = productRepository.GetMany(a => a.Id % 2 == 0);

I changed this to:

var results = productRepository.GetMany(a => a.Price > 0m);

And it worked immediately. So this looks like it caused by trying to query on the ID of the entity. I am using the HiLo ID generator for NHibernate. Any idea why I can't query on the ID value?

**ANOTHER EDIT

This also doesn't work:

var results = productRepository.GetMany(a => a.UnitsInStock % 2 == 0);

yet this does:

var results = productRepository.GetMany(a => a.UnitsInStock > 400 == 0);

Units in stock is just an integer column. It looks to me like the modulo operator doesn't work with NHibernate and Linq.


You must call ToFuture() or ToList() for actual fetch data from database.

private readonly ISession _context;

public IEnumerable<T> GetMany(Expression<Func<T, bool>> whereExpression)
{
    return _context.Query<T>().Where(whereExpression).ToFuture();
}

or

private readonly ISession _context;

public IEnumerable<T> GetMany(Expression<Func<T, bool>> whereExpression)
{
    return _context.Query<T>().Where(whereExpression).ToList();
}

UPDATED

According to updated information: this is limitations of linq for NHibernate provider implementation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜