Daniel Williams
Daniel Williams

Reputation: 9304

Getting duplicate results in query

I have Three tables A, B, and C. A is a parent table with mutiple child records in B and C.

When I query into A I am getting too many records, as though FNH is doing a Cartesian product.

My query is of the form:

var list = session.Query<A>()
  .Fetch(a=> a.Bs)
  .Fetch(a=> a.Cs)

Where Bs is the IList property of A, and Cs is the IList property of A.

I should get only as many Bs as relate to A, and only as many Cs relate to A. Instead I get BxC elements of each.

Is there a better way to load these? I am pretty sure I avoided this exact issue in the past, but don't see it in my old example code.

Upvotes: 1

Views: 167

Answers (2)

Firo
Firo

Reputation: 30813

I'm not sure if this is a NH bug or a mapping issue, however the query could be optimised to

session.Query<A>()
    .Fetch(a=> a.Bs)
    .ToFuture();

var results = session.Query<A>()
    .Fetch(a=> a.Cs)
    .ToFuture()
    .ToList();

Upvotes: 2

jishi
jishi

Reputation: 24624

You could use a Transformer to get a distinct result:

var list = session.Query<A>()
  .Fetch(a=> a.Bs)
  .Fetch(a=> a.Cs)
  .SetResultTransformer( Transformers.DistinctRootEntity )

This is NH3.2 syntax, for 2.1 you need to use new DistinctRootEntityTransformer() (I think) as parameter to SetResultTransformer instead.

Upvotes: 2

Related Questions