【发布时间】:2014-08-29 23:23:29
【问题描述】:
好的,我已经看到了一些与此类似的问题,但答案要么让我感到困惑,要么似乎完全过度设计,所以我想问我自己的问题。
我有一个名为 Tree 的类,它有一个 Plot 类的对象属性,它有一个 Year 类的对象属性,它有一个 Series 类的对象属性,它有一个名为 Id 的字符串属性。总结如下。
public class Tree {
public virtual Plot Plot { get; set; }
// other properties...
}
public class Plot {
public virtual Year Year { get; set; }
// other properties...
}
public class Year {
public virtual Series Series { get; set; }
// other properties...
}
public class Series {
public virtual string Id { get; set; }
// other properties...
}
这些类中的每一个都对应一个数据库的表,属性对应于外键字段(例如Trees表有一个字段叫PlotKey,它引用了Plots表中的一条记录)。我要做的就是从数据库中加载所有树,其相应系列的 ID 为“Adrian_2012”或“IPED Sample”。我认为使用以下代码会很容易:
IList<Tree> trees = session.CreateCriteria<Tree>()
.Add(Expression.Or(
Expression.Eq("Plot.Year.Series.Id", "Adrian_2012")
Expression.Eq("Plot.Year.Series.Id", "IPED Sample")
))
.List<Tree>();
但这是抛出:“NHibernate.Exceptions.GenericADOException:无法执行查询”。我尝试过使用 Expression.Disjunction,我尝试过使用 Aliases、Restrictions 和 SimpleExpressions,并且我知道不会发生像未映射的属性或拼写错误的标准这样的愚蠢行为。我见过的唯一可能有帮助的是 ISession.QueryOver() 函数,但我对 lambda 表达式感到非常困惑。有没有人可以为我提供一个只使用像上面这样简单的 CreateCriteria 语句的解决方案?
提前致谢!
【问题讨论】:
标签: c# nhibernate nhibernate-criteria nested-properties