views:

214

answers:

1

Hi!

My entities look something like that (simplified):

public class Person
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public IList<Department> Departments { get; set; }
}

public class Department
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

I'm querying the database through criteria api for all persons that have a department with a certain name that should match a like-pattern.

It happens that a person contains two or more departments whose names contain the same character sequence which is used by the query. Therefore the same person is returned multiple times. To surpress this, I know that I can use criteria.SetResultTransformer(Transformers.DistinctRootEntity); but this works only as long as the result is not paged.

When I'm paging the result I don't only need to get the first page but I also need to know how many entities there are in total. Unfortunately the result transformer does not work when calling criteria.SetProjection(Projections.RowCount()) as there is no result to be transformed.

Can I somehow avoid retrieving the whole list of person with the result transformer and then manually taking the right part out of the collection?

Best Regards
Oliver Hanappi

+1  A: 

You need to include distinct in your sql request. Some information you can find here. Second answer mostly

Sly
When I use distinct only columns will be returned which are included in the distinct projection. Will NHibernate query the database another time in order to get all data?
Oliver Hanappi
What all data? As I can see you need only numbe of rows or what do you mean?
Sly
Ah, I see your point! There is still a problem: When getting paged results, there will be retrieved e.g. 25 persons. After that, the result transformer is being applied and e.g. 3 persons are removed. Then my query would return 22 persons or am I wrong?
Oliver Hanappi
If your are using ResultTransformer then yes, it will work in that way. But when you do use Projections then there is no actions after sql has executed.
Sly