private SqlString GetWithClause(IDictionary <string, IFilter> enabledFilters, ref SqlString withClauseFragment, Join join, IJoinable last) { string on = join.AssociationType.GetOnCondition(join.Alias, factory, enabledFilters); var withConditions = new List <object>(); if (!string.IsNullOrEmpty(on)) { withConditions.Add(on); } if (last != null && IsManyToManyRoot(last) && ((IQueryableCollection)last).ElementType == join.AssociationType) { // the current join represents the join between a many-to-many association table // and its "target" table. Here we need to apply any additional filters // defined specifically on the many-to-many string manyToManyFilter = ((IQueryableCollection)last) .GetManyToManyFilterFragment(join.Alias, enabledFilters); if (!string.IsNullOrEmpty(manyToManyFilter)) { withConditions.Add(manyToManyFilter); } } else if (string.IsNullOrEmpty(on)) { // NH Different behavior : NH1179 and NH1293 // Apply filters for entity joins and Many-To-One association var enabledFiltersForJoin = ForceFilter ? enabledFilters : FilterHelper.GetEnabledForManyToOne(enabledFilters); if (ForceFilter || enabledFiltersForJoin.Count > 0) { withConditions.Add(join.Joinable.FilterFragment(join.Alias, enabledFiltersForJoin)); } } if (withClauseFragment != null && !IsManyToManyRoot(join.Joinable)) { withConditions.Add(withClauseFragment); withClauseFragment = null; } return(SqlStringHelper.JoinParts(" and ", withConditions)); }