I am using Nhibernate v2.1.2.4000. With many-to-many relationship between Posts an Tags I have the query:
tags
.Select(t => new { Name = t.Name, Count = t.Posts.Count })
.OrderBy(x => x.Count);
Ordering anonymous type fails (reference not set to an instance of an object). Is this issue something related to LinqToNH? What can be the source of this error? What is the solution? If it is something related to LinqToNH then how it can be solved with some other option (ie Criteria API)?
EDIT: When I try Adam's ICriteria option, SqlProfiler says executed script is:
SELECT this_.Name as y0_, count(this_.Id) as y1_ FROM Tag this_ GROUP BY this_.Name ORDER BY count(this_.Id) asc
Mapping for Tag:
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();
}
}