Perhaps there's something wrong with my Linq query, or I'm running into something NHibernate doesn't support. Either way, this is a strange one. Here's my query that does work:
// 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));
This produces the query:
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
I don't really like the weird cast()
thing going on, but I doubt that slows anything down.
However, I need to add a second order by as well. The results need to be sorted by the ingredient name next. So I've tried the obvious:
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));
This throws the exception:
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
What am I doing wrong?