开发者

NHibernate FetchMode

开发者 https://www.devze.com 2022-12-29 10:53 出处:网络
I have an object Parent which has a list of Children: class Parent {Id, Name, IList<Child> children}

I have an object Parent which has a list of Children:

class Parent {Id, Name, IList<Child> children}
class Child {开发者_运维知识库Id, Name}

I need to select all parents where there is a condition for their children but I do not want to get duplicate rows (don't want the children details to appear in select list)

Here is the code:

session.CreateCriteria(typeof(Parent))
.SetFetchMode("children", FetchMode.Select);
.CreateCriteria("children").Add(Subqueries.PropertyIn("Id", {1,2,3,4}))
.List<Parent>();

The query adds all proprties of child class to select list which results in duplicate Parents.

Is there any way I can select all parents without having the child details in the select list?

Thanks


One possible solution:

session.CreateCriteria<Parent>()
       .CreateCriteria("children")
       .Add(Subqueries.PropertyIn("Id", {1,2,3,4}))
       .SetResultTransformer(Transformers.DistinctRootEntity)
       .List<Parent>();
0

精彩评论

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