我正在尝试获取Product
为空的所有CheckedOutToGuid
或者是当前用户的Guid。我试图使用以下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
为空的时候:-( 这是导致错误的原因。错误消息导致我的方向错误。
答案 0 :(得分:5)
NHibernate试图将Guid.Equals
转换为SQL表达式。 NHibernate不会将Guid.Equals
识别为可以转化为SQL的东西,因此也是例外。
如果您将该行更改为:
.Where(x => (x.CheckedOutByGuid == null) || user.UserGuid == x.CheckedOutByGuid)
一切都应该正常。