/// <summary> /// Retrieves a list of groups matching the criteria specified via method parameters. /// </summary> /// <param name="category">Category name.</param> /// <param name="categoryId">Category Id</param> /// <param name="pageNumber">Page number.</param> /// <param name="pageSize">Page size.</param> /// <param name="recordCount">Record count.</param> /// <returns>List of groups.</returns> public List<MonoSoftware.MonoX.Repositories.SnGroupDTO> GetPopularGroups(string category, Guid categoryId, int pageNumber, int pageSize, out int recordCount) { RelationPredicateBucket filter = new RelationPredicateBucket(); //introduced to filter out groups by languages and applications filter.Relations.Add(SnGroupEntity.Relations.SnGroupCategoryEntityUsingGroupCategoryId, JoinHint.Left); //Note: MonoX supports the multi application environment so general filter for all DB access calls should contain the application id filter filter.PredicateExpression.Add(SnGroupCategoryFields.ApplicationId == MonoSoftware.MonoX.Repositories.MembershipRepository.GetInstance().GetApplicationId()); //Note: MonoX in supports the multi language environment so general filter for all DB access calls should contain the language id filter filter.PredicateExpression.Add(SnGroupCategoryFields.LanguageId == LocalizationUtility.GetCurrentLanguageId()); //Filter groups by category if (categoryId != Guid.Empty) { filter.PredicateExpression.Add(SnGroupFields.GroupCategoryId == categoryId); } if (!String.IsNullOrEmpty(category)) { filter.PredicateExpression.Add(SnGroupCategoryFields.Slug == category); } IPrefetchPath2 prefetch = new PrefetchPath2((int)EntityType.SnGroupEntity); prefetch.Add(SnGroupEntity.PrefetchPathSnGroupCategory); //Fetch a record from the members table only for the current user so his status could be read Guid uid = SecurityUtility.GetUserId(); if (!Guid.Empty.Equals(uid)) { PredicateExpression memberFilter = new PredicateExpression(SnGroupMemberFields.UserId == uid); prefetch.Add(SnGroupEntity.PrefetchPathSnGroupMembers, 1, memberFilter); } #region Popular groups sorter const string memberCountField = "MemberCountField"; const string memberCountTableName = "MemberCountTable"; EntityFields2 memberFields = new EntityFields2(2); memberFields.DefineField(SnGroupMemberFields.GroupId, 0); memberFields.DefineField(SnGroupMemberFields.Id, 1, memberCountField, AggregateFunction.Count); DerivedTableDefinition memberCountTable = new DerivedTableDefinition(memberFields, memberCountTableName, null, new GroupByCollection(memberFields[0])); IDynamicRelation memberCountRelation = new DynamicRelation(memberCountTable, JoinHint.Right, MonoSoftware.MonoX.DAL.EntityType.SnGroupEntity, String.Empty, SnGroupMemberFields.GroupId.SetObjectAlias(memberCountTable.Alias) == SnGroupFields.Id); filter.Relations.Add(memberCountRelation); ISortExpression sorter = new SortExpression(new SortClause(new EntityField2(memberCountField, null).SetObjectAlias(memberCountTableName), null, SortOperator.Descending)); #endregion EntityCollection<SnGroupEntity> groups = new EntityCollection<SnGroupEntity>(); //Fetch the group collection FetchEntityCollection(groups, filter, 0, sorter, prefetch, pageNumber, pageSize); //Fetch the group total count used by paging recordCount = GetDbCount(groups, filter); //Transfer group entities to the DTO List<MonoSoftware.MonoX.Repositories.SnGroupDTO> toReturn = groups.Select(group => new MonoSoftware.MonoX.Repositories.SnGroupDTO(group)).ToList<MonoSoftware.MonoX.Repositories.SnGroupDTO>(); return toReturn; }