开发者

Eagerly fetch multiple collection: differences between QueryOver and Query

I have the classic situation Orders/OrderLines. I wanted to fetch some orders and eagerly load the orderslines with an outer join. I have noticed that if I use QueryOver:

var orders2 = session.QueryOver<Domain.Order>()
   .Where(x => x.Company == "HBP00").And(x => x.Numbe开发者_开发技巧r == "VI11001680")
   .Fetch(x => x.OrderLines).Eager
   .List();

I get a cartesian product.

orders2 contains 11 rows even if there's just one order, but infact it has got 11 orderlines.

If I use Query:

var orders1 = session.Query<Domain.Order>()
    .Where(x => x.Company == "HBP00" && x.Number == "VI11001680")
    .Fetch(x => x.OrderLines)
    .ToList();

everything works properly. The funny thing is I've analyzed the query and they're identical. Is there anything I have to know not to get a cartesian product eagerly loading a collection with QueryOver?


I've found a solution reading this blog.

Apparently, you need .TransformUsing(Transformers.DistinctRootEntity) with CreateQuery, CreateCriteria or QueryOver but you do not need it with Query.

Here's how I fixed it:

var orders2 = session.QueryOver<Domain.Order>()
   .Where(x => x.Company == "HBP00").And(x => x.Number == "VI11001680")
   .Fetch(x => x.OrderLines).Eager
   .TransformUsing(Transformers.DistinctRootEntity)
   .List();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜