Get children count with restrictions using QueryOver on NHibernate

∥☆過路亽.° 提交于 2019-11-29 20:40:19

问题


How can I do the following using QueryOver and no Formula Fields.

I have the following parent/child relationship

public class Club {

public string Name {get; set;}
public IList<Membership> Memberships {get; set;}
}

public class Membership {

public boolean Cancelled {get; set;}
public Club Club {get; set;}
}

I have the following query that returns 15 Clubs and transforms the results to a DTO, I need to add to this query a RowCount of Memberships that belong to each club and that are not Cancelled.

IEnumerable<ClubIndexViewModelLineSummary> results = _querySession.QueryOver<Club>()
                    .OrderBy(c => c.IsActive).Desc
                    .OrderBy(c => c.Name).Asc
                    .SelectList(list => list
                                            .Select(c => c.Id).WithAlias(() => sum.Id)
                                            .Select(c => c.Name).WithAlias(() => sum.Name)
                                            .Select(c => c.IsActive).WithAlias(() => sum.IsActive)
                                            .Select(c => c.NumberOfWeeks).WithAlias(() => sum.NumberOfWeeks))
                    .TransformUsing(Transformers.AliasToBean<ClubIndexViewModelLineSummary>())
                    .Skip(start)
                    .Take(15)
                    .Future<ClubIndexViewModelLineSummary>();

回答1:


After some more research, this is the resulting query that works:

Club clubAlias = null;

                IEnumerable<ClubIndexViewModelLineSummary> results = 
                 _querySession.QueryOver<Club>(() => clubAlias)
                    .OrderBy(c => c.IsActive).Desc
                    .OrderBy(c => c.Name).Asc
                    .SelectList(list => list
                                            .Select(c => c.Id).WithAlias(() => sum.Id)
                                            .Select(c => c.Name).WithAlias(() => sum.Name)
                                            .Select(c => c.IsActive).WithAlias(() => sum.IsActive)
                                            .Select(c => c.NumberOfWeeks).WithAlias(() => sum.NumberOfWeeks)
                                            .SelectSubQuery
                                            (
                                                QueryOver.Of<ClubMembership>()
                                                    .Where(c => c.Canceled == false)
                                                    .And(c=> c.Club.Id == clubAlias.Id)
                                                    .ToRowCountQuery()
                                            ).WithAlias(()=> sum.ActiveMembers))

                    .TransformUsing(Transformers.AliasToBean<ClubIndexViewModelLineSummary>())
                    .Skip(start)
                    .Take(15)
                    .Future<ClubIndexViewModelLineSummary>();


来源:https://stackoverflow.com/questions/6333217/get-children-count-with-restrictions-using-queryover-on-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!