Linq to NHibernate - Anonymous Type Ordering

I am using Nhibernate v2.1.2.4000. With many-to-many relationships between posts, tags I have a request:

tags
.Select(t => new { Name = t.Name, Count = t.Posts.Count })
.OrderBy(x => x.Count);

An anonymous type order fails (the link is not set to an instance of the object). Is this problem related to LinqToNH? What could be causing this error? What is the solution? If this is related to LinqToNH, then how can it be solved using any other option (for example, criteria API)?

EDIT: When I try to use the Adam ICriteria parameter, SqlProfiler says the script executed:

SELECT this_.Name as y0_, count(this_.Id) as y1_ FROM Tag this_ GROUP BY this_.Name ORDER BY count(this_.Id) asc

Tag display:

public class TagMap : ClassMap<Tag>
{
    public TagMap()
    {
        Table("Tag");
        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.Name);
        HasManyToMany(x => x.Posts)
            .Table("PostTags")
            .ChildKeyColumn("Post")
            .ParentKeyColumn("Tag")
            .Cascade.None().Inverse();
    }
}
+3
source share
2 answers

NHibernate.Linq NHibernate 2.1.2.4000 , . HQL ICriteria NHibernate 3.0, , Linq Select, ToList.

tags
    .Select(t = new { t.Name, t.Posts.Count })
    .ToList()
    .OrderBy(x => x.Count);

, NHibernate.Linq .

, , /, .

EDIT: ICriteria :

var tags = session.CreateCriteria(typeof(Tag), "tag")
    .SetProjection(
        Projections.GroupProperty("tag.Name"),
        Projections.Count("tag.Posts"))
    .AddOrder(Order.Asc(Projections.Count("tag.Posts")))
    .List();

EDIT: SQL, Arch. . .

var tags = session.CreateCriteria(typeof(Tag), "tag")
    .CreateCriteria("tag.Posts", "post")
    .SetProjection(
        Projections.GroupProperty("tag.Name"),
        Projections.Count("post.Id"))
    .AddOrder(Order.Asc(Projections.Count("post.Id")))
    .List();

SQL, , ...

SELECT this_.Name as y0_, count(post1_.Id) as y1_ FROM Tag this_ inner join Post_Tags posts3_ on this_.Id=posts3_.Tag inner join Post post1_ on posts3_.Post=post1_.Id GROUP BY this_.Name ORDER BY count(post1_.Id) asc
+2

, . 2.1.2.4, .

: Count Count()

+1

Source: https://habr.com/ru/post/1777769/


All Articles