/// <summary> /// Returns query with sub-query with filters and sorting applied. /// </summary> /// <typeparam name="TRoot">The type of the root.</typeparam> /// <typeparam name="TSubType">The type of the sub type.</typeparam> /// <param name="query">The query.</param> /// <param name="filter">The filter.</param> /// <param name="order">The order.</param> /// <param name="orderDescending">if set to <c>true</c> order by descending.</param> /// <param name="pageNumber">The page number.</param> /// <param name="itemsPerPage">The items per page.</param> /// <returns>Query with sub-query with filters and sorting applied</returns> public static IQueryOver <TRoot, TSubType> ApplySubQueryFilters <TRoot, TSubType>(this IQueryOver <TRoot, TSubType> query, Expression <Func <TSubType, bool> > filter = null, Expression <Func <TSubType, dynamic> > order = null, bool orderDescending = false, int?pageNumber = null, int?itemsPerPage = null) where TRoot : Entity where TSubType : Entity { // If added paging, adding subQuery if (itemsPerPage > 0) { var idSubQuery = (QueryOver <TRoot, TSubType>)QueryOver.Of <TSubType>() .ApplyFilters(filter, order, orderDescending, pageNumber, itemsPerPage) .Select(Projections.Property <TSubType>(l => l.Id)); return(query .WithSubquery .WhereProperty(m => m.Id) .In(idSubQuery) .TransformUsing(Transformers.DistinctRootEntity)); } // If no paging, subquery is not required return(query .ApplyFilters(filter, order, orderDescending, pageNumber, itemsPerPage) .TransformUsing(Transformers.DistinctRootEntity)); }