Nhibernate Linq - Choose Top Rated Products

Perhaps the question covers the existing one, but I still cannot find a solution.

I have 2 tables A and B. B has a rating - this is a very simple rating, so it just refers to the user and refers to an element in table A. I want to select the most popular elements from table A.

So, I tried this:

var innerQuery = from csr in session.Query<Rating>()
                                 group csr by csr.ItemId into scrg
                                 orderby scrg.Count() descending
                                 select scrg.Key;

            var result = from cs in session.Query<Item>() where innerQuery.Contains(cs.Id) select cs;

            return result.Take(maxToReturn).ToList<Item>();

It’s important for me to return List<Item>

But I have an exception:

Action: Index Exception: System.NotImplementedException: Cannot use group by with the ContainsResultOperator result operator.
at NHibernate.Linq.GroupBy.AggregatingGroupByRewriter.FlattenSubQuery(QueryModel queryModel, QueryModel subQueryModel)
at NHibernate.Linq.GroupBy.AggregatingGroupByRewriter.ReWrite(QueryModel queryModel)
at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root)
at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitSubQueryExpression(SubQueryExpression expression)
at ............

Any ideas how to do this?

UPDATE

The My Item class is not related to the rating at all, I just count it when the item is displayed, because it can be evaluated by anyone (so it needs to be updated quite often), but can I add relationships if this helps?

Thanks guys,

+4
2

. , Linq to Nhibernate . , -, .

, .Query .

, , ToList, , , .

 var innerQuery = (from csr in session.Query<Rating>()
                             group csr by csr.ItemId into scrg
                             orderby scrg.Count() descending
                             select scrg.Key).ToList();

, nhibernate. , API- where, . .

+2

:

            var innerQuery = (from csr in session.Query<Rating>()
                         group csr by csr.Id into items
                         orderby items.Count()
                         select items.Key).ToList();

            var result = (from cs in session.Query<Item>()
                       where innerQuery.Contains(cs.Id)
                       select cs).ToList<Item>();

, , . , , , ;)

+1

All Articles