I am trying to get the total comments for an entity yet I am getting back incorrect results.
How would I get a Task and its comments counts (eagerly loaded)
var tasks = _session.QueryOver<Task>(() => taskAlias)
.Where(x => x.OrganizationID == null)
.Fetch(x => x.AssignedUser).Eager
.Fetch(x => x.Owner).Eager
.Fetch(x => x.Comments).Eager
.List();
This returns:
Task.ID 1 Comments 3
Task.ID 1 Comments 3
Task.ID 1 Comments 3
Task.ID 2 Comments 2
Task.ID 2 Comments 2
Task.ID 3 Comments 1
I want:
Task.ID 1 Comments 3
Task.ID 2 Comments 2
Task.ID 3 Comments 1
My fluent mappings are:
HasMany(x => x.Comments).Table("tComments").ForeignKeyConstraintName("fT_Task_ID").KeyColumn("fC_Resource_ID").Where("fC_Type = 'Task'").ReadOnly();
If I understand your issue correctly, the problem is that your getting the correct data back, it’s just duplicating at the root level. If this is the case, change your query to:
When you have an entity and it contains a collection of entities and you tell NHibernate to fetch the collections eagerly the SQL that is generated will essentially return as:
The DistinctRootEntityResultTransformer is what we use to tell NHibernate that the root element should be unique. As your original code didn’t have the transformer your root elements are duplicated.