4

我正在尝试获取null 或当前用户的 Guid 的所有Product位置。CheckedOutToGuid我尝试使用以下 QueryOver:

    public IEnumerable<IProduct> GetProducts(IUser user, Expression<Func<IProduct, bool>> predicate)
    {
        return Context.GetSession().QueryOver<Product>()
            .Where(x => (x.CheckedOutByGuid == null) || Guid.Equals(user.UserGuid, x.CheckedOutByGuid)
            .OrderBy(x => x.Version).Desc
            .OrderBy(x => x.DisplayName).Asc.List();
    }

但我收到一个错误:

Atf.NUnit.Model.TestDatabase.TestModule1():
System.Exception : Unrecognised method call: System.Object:Boolean Equals(System.Object, System.Object)

这个错误的原因是什么?我尝试删除(x.CheckedOutByGuid == null)以便不需要 OR,但仍然得到相同的错误。

更新

按照@Andrew Whitaker建议,我换了一行

   .Where(x => (x.CheckedOutByGuid == null) || (user.UserGuid == x.CheckedOutByGuid))

现在这会导致以下异常:

Atf.NUnit.Model.TestDatabase.TestModule1():
NHibernate.QueryException : could not resolve property: user of: Atf.Model.Internal.Product
bei NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(String propertyName) in p:\nhibernate-core\src\NHibernate\Persister\Entity\AbstractPropertyMapping.cs:Zeile 37.
bei NHibernate.Persister.Entity.AbstractEntityPersister.GetSubclassPropertyTableNumber(String propertyPath) in p:\nhibernate-core\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 1776.
bei NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns(String alias, String propertyName) in p:\nhibernate-core\src\NHibernate\Persister\Entity\BasicEntityPropertyMapping.cs:Zeile 31.
bei NHibernate.Persister.Entity.AbstractEntityPersister.ToColumns(String alias, String propertyName) in p:\nhibernate-core\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:Zeile 1742.
bei NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(ICriteria subcriteria, String propertyName) in p:\nhibernate-core\src\NHibernate\Loader\Criteria\CriteriaQueryTranslator.cs:Zeile 590.
bei NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection(ICriteria subcriteria, String propertyName) in p:\nhibernate-core\src\NHibernate\Loader\Criteria\CriteriaQueryTranslator.cs:Zeile 561.
bei NHibernate.Criterion.CriterionUtil.GetColumnNamesUsingPropertyName(ICriteriaQuery criteriaQuery, ICriteria criteria, String propertyName) in p:\nhibernate-core\src\NHibernate\Criterion\CriterionUtil.cs:Zeile 64.
bei NHibernate.Criterion.CriterionUtil.GetColumnNames(String propertyName, IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria, IDictionary`2 enabledFilters) in p:\nhibernate-core\src\NHibernate\Criterion\CriterionUtil.cs:Zeile 20.
bei NHibernate.Criterion.PropertyExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) in p:\nhibernate-core\src\NHibernate\Criterion\PropertyExpression.cs:Zeile 74.
bei NHibernate.Criterion.LogicalExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) in p:\nhibernate-core\src\NHibernate\Criterion\LogicalExpression.cs:Zeile 77.
bei NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2 enabledFilters) in p:\nhibernate-core\src\NHibernate\Loader\Criteria\CriteriaQueryTranslator.cs:Zeile 202.
bei NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) in p:\nhibernate-core\src\NHibernate\Loader\Criteria\CriteriaJoinWalker.cs:Zeile 58.
bei NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) in p:\nhibernate-core\src\NHibernate\Loader\Criteria\CriteriaLoader.cs:Zeile 41.
bei NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) in p:\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:Zeile 1948.
bei NHibernate.Impl.CriteriaImpl.List(IList results) in p:\nhibernate-core\src\NHibernate\Impl\CriteriaImpl.cs:Zeile 265.
bei NHibernate.Impl.CriteriaImpl.List[T]() in p:\nhibernate-core\src\NHibernate\Impl\CriteriaImpl.cs:Zeile 276.
bei NHibernate.Criterion.QueryOver`1.List() in p:\nhibernate-core\src\NHibernate\Criterion\QueryOver.cs:Zeile 67.
bei NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver<TRoot>.List() in p:\nhibernate-core\src\NHibernate\Criterion\QueryOver.cs:Zeile 214.
bei Atf.Model.Database.DatabasePersistence.GetProducts(IUser user, Expression`1 predicate) in D:\my_workspaces\visualstudioprojects\MyTestFramework\Atf.Model\Database\DatabasePersistence.cs:Zeile 247.
bei Atf.NUnit.Model.TestDatabase.TestModule1() in D:\my_workspaces\visualstudioprojects\MyTestFramework\Atf.zNUnit.Model\TestDatabase.cs:Zeile 59.

好吧,然后我尝试了这个:

    public IEnumerable<IProduct> GetProducts(IUser user, Expression<Func<IProduct, bool>> predicate)
    {
        Guid g = user.UserGuid;
        return Context.GetSession().QueryOver<Product>()
            .Where(x => (x.CheckedOutByGuid == null) || (g == x.CheckedOutByGuid))
            .OrderBy(x => x.Version).Desc
            .OrderBy(x => x.DisplayName_DE).Asc.List();
    }

这是我发现它user为空的时候:-(这是错误的原因。错误消息将我引向了完全错误的方向。

4

1 回答 1

5

NHibernate 正在尝试Guid.Equals转换为 SQL 表达式。NHibernate 不认为Guid.Equals它可以变成 SQL,因此是例外。

如果您将该行更改为:

.Where(x => (x.CheckedOutByGuid == null) || user.UserGuid == x.CheckedOutByGuid)

一切都应该正常工作。

于 2013-05-06T15:19:27.790 回答