- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
也许我的 Linq 查询有问题,或者我遇到了 NHibernate 不支持的问题。无论哪种方式,这都是一个奇怪的问题。这是我的查询确实有效:
// Query for all ingredients, most used ingredients first
var ingredients = (from ing in session.Query<Ingredients>()
orderby ((from p in session.Query<RecipeIngredients>()
where p.Ingredient == ing
select p.RecipeIngredientId).Count()) descending
select new IngredientSource(ing.IngredientId, ing.DisplayName));
这会产生查询:
select
ingredient0_.IngredientId as col_0_0_,
ingredient0_.DisplayName as col_1_0_
from ingredients ingredient0_
order by (select cast(count(recipeingr1_.RecipeIngredientId) as int4) from recipeingredients recipeingr1_ where recipeingr1_.IngredientId=ingredient0_.IngredientId) desc
我真的不喜欢发生的奇怪的 cast()
事情,但我怀疑这会减慢速度。
不过,我还需要添加一个第二个订单。接下来需要按成分名称 对结果进行排序。所以我尝试了显而易见的方法:
var ingredients = (from ing in session.Query<Ingredients>()
orderby ((from p in session.Query<RecipeIngredients>()
where p.Ingredient == ing
select p.RecipeIngredientId).Count()) descending,
ing.DisplayName
select new IngredientSource(ing.IngredientId, ing.DisplayName));
这会抛出异常:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException was unhandled
HResult=-2146232832
Message=Exception of type 'Antlr.Runtime.MismatchedTreeNodeException' was thrown. [.Select[KitchenPC.DB.Models.Ingredients,KitchenPC.Context.IngredientSource](.ThenBy[KitchenPC.DB.Models.Ingredients,System.String](.OrderByDescending[KitchenPC.DB.Models.Ingredients,System.Int32](NHibernate.Linq.NhQueryable`1[KitchenPC.DB.Models.Ingredients], Quote((ing, ) => (.Count[System.Guid](.Select[KitchenPC.DB.Models.RecipeIngredients,System.Guid](.Where[KitchenPC.DB.Models.RecipeIngredients](NHibernate.Linq.NhQueryable`1[KitchenPC.DB.Models.RecipeIngredients], Quote((p, ) => (Equal(p.Ingredient, ing))), ), Quote((p, ) => (p.RecipeIngredientId)), ), ))), ), Quote((ing, ) => (ing.DisplayName)), ), Quote((ing, ) => (new IngredientSource(ing.IngredientId, ing.DisplayName, ))), )]
Source=NHibernate
QueryString=.Select[KitchenPC.DB.Models.Ingredients,KitchenPC.Context.IngredientSource](.ThenBy[KitchenPC.DB.Models.Ingredients,System.String](.OrderByDescending[KitchenPC.DB.Models.Ingredients,System.Int32](NHibernate.Linq.NhQueryable`1[KitchenPC.DB.Models.Ingredients], Quote((ing, ) => (.Count[System.Guid](.Select[KitchenPC.DB.Models.RecipeIngredients,System.Guid](.Where[KitchenPC.DB.Models.RecipeIngredients](NHibernate.Linq.NhQueryable`1[KitchenPC.DB.Models.RecipeIngredients], Quote((p, ) => (Equal(p.Ingredient, ing))), ), Quote((p, ) => (p.RecipeIngredientId)), ), ))), ), Quote((ing, ) => (ing.DisplayName)), ), Quote((ing, ) => (new IngredientSource(ing.IngredientId, ing.DisplayName, ))), )
StackTrace:
at NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException()
at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at KitchenPC.DB.DatabaseAdapter.LoadIngredientsForIndex() in c:\KitchenPC\DB\DatabaseAdapter.cs:line 172
我做错了什么?
最佳答案
我已将 Linq 查询转换为 ICriteria
( QueryOver<>
) 查询,这似乎有效。但是,代码相当困惑。或者,很棒,具体取决于您的兴趣。
Models.Ingredients ing = null;
int? count = null;
var popularity = QueryOver.Of<Models.RecipeIngredients>()
.Where(p => p.Ingredient.IngredientId == ing.IngredientId)
.ToRowCountQuery();
var ingredients = session.QueryOver<Models.Ingredients>(() => ing)
.SelectList(list => list
.Select(p => p.IngredientId)
.Select(p => p.DisplayName)
.SelectSubQuery(popularity).WithAlias(() => count)
)
.OrderByAlias(() => count).Desc()
.ThenBy(p => p.DisplayName).Asc()
.List<Object[]>()
.Select(i => new IngredientSource((Guid)i[0], (String)i[1]));
这会产生与原始查询非常相似的查询:
SELECT
this_.IngredientId as y0_,
this_.DisplayName as y1_,
(SELECT count(*) as y0_ FROM recipeingredients this_0_ WHERE this_0_.IngredientId = this_.IngredientId) as y2_
FROM ingredients this_
ORDER BY y2_ desc, this_.DisplayName asc
关于c# - 将多个 ORDER BY 子句添加到 NHibernate 查询会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586747/
我一直在为初学者阅读 nhibernate 3.0 并阅读了一些常见错误(我犯了一些错误) 我想知道将一个或多个记录设为只读有哪些策略。现在我取回所有行并循环遍历它们,使它们通过 session.Re
我们有一个使用 NHibernate 的相当健壮的系统,我们正在从单个数据库服务器迁移到拥有两台服务器,一台用于管理,另一台用于我们面向公众的网站。这主要是为了让我们可以使用不会影响当前站点的工作流来
我在尝试构建一个时遇到以下错误 session 工厂: PersistenceTests.Can_Map_Orders_To_Database : Failed System.IndexOutOfRa
如果客户有很多订单附加到他们。您将如何使用 NHibernate 延迟加载订单列表。 是不是需要设置映射文件?任何帮助或示例都会很棒。 最佳答案 Chris 的建议是我会怎么做,但是如果您想在运行时执
我正在尝试使用 HQL 对一个简单的查询进行分页,并将总行数作为同一查询的一部分进行检索。 我的查询很简单... var members = UnitOfWork.CurrentSession.Cre
我有旧版数据库,存储的日期表示无日期为9999-21-31, 列Till_Date的类型为DateTime not-null="true"。 在应用程序中,我要构建持久化类,将no-date表示为nu
您可以指定命名空间和程序集以使用 HBM 文件顶部的类型: 您可以在同一个映射文件中使用来自多个程序集/命名空间的类型,如果可以,这样做的语法是什么? 最佳答案 您可以从 HBM 文件的顶部删除默认
如何强制 NHibernate 在多对多集合上执行 RIGHT 外连接或 INNER 连接而不是 LEFT 外连接? 我想这样做的原因是因为过滤应用于集合元素。使用左连接,您将获得与未过滤查询相同的返
我们开始在我的工作场所使用NHibernate,包括从映射生成模式。我们的DBA想要的一件事是主键和外键关系的名称一致。我已经能够设置FK约束名称,但是在的文档中看,它似乎不存在命名主键约束的方法。
我需要NHibernate来执行这样的查询: SELECT * FROM Users ORDER BY Func(FirstName, LastName) Standart NHibernate Or
假设在一个实体中有属性 id、用户名、年龄、地址。现在我只想要 id 和 username 并使用此代码。 投影可以从查询中返回实体列表以外的内容。 var proj = Projections.Pr
我花了很长时间,但我终于让 nHibernate 的 Hello World 工作了。在我做了“延迟加载”之后它起作用了。老实说,我无法告诉您为什么这一切都有效,但确实如此,现在我正在阅读您不需要延迟
假设您有两个类,Person 和 Address。 Person 有一个对 Address 的引用,如下所示: public class Person { public virtual Addre
我在 NHibernate 引用文档 中阅读第 10 章“只读实体”如下: http://nhibernate.info/doc/nh/en/index.html#readonly 但不幸的是我不知道
有谁知道 NHibernate 是否支持从存储过程返回输出参数?我在文档中进行了搜索,但无法真正找到任何可以确认的内容。 最佳答案 我面临同样的问题。 NHibernate 不允许您以这种方式使用存储
简而言之,什么工作得更快: SessionFactory 预编译 XML 配置,或 流畅的NHibernate提供 以编程方式配置? 最佳答案 我个人的经验是,Configuration 对象的构建(
我的域类具有如下所示的集合: private List _foos = new List(); public virtual ReadOnlyCollection Foos { get { retur
当我有一个带有一对多子集合的实体对象时,我需要查询一个特定的子对象,是否有我还没有想出的功能或一些巧妙的模式来避免 NHibernate获取整个子集合? 例子: class Parent {
在我的域中,员工和部门具有一对多的双向关系;为了让子员工同步这个,我有一个“内部”访问字段,用于部门中员工的集合(NHibernate 的 Iesi),否则它将是只读公共(public)的。像这样:
我有一个 nhibernate 自定义类型,我想用 Fluent NHibernate 映射它。 HBM 映射如下所示。 Services.Data.DateConventionTyp
我是一名优秀的程序员,十分优秀!