MonoX support board

Start the conversation, ask questions and share tips and solutions with fellow developers.

Non-registered users can only browse through our support boards. Please register now if you want to post your questions. It takes a second and it is completely free. Alternatively, you can log in without registration using your credentials at major sites such as Google, Microsoft Live, OpenId, Facebook, LinkedIn or Yahoo.

Get groups from 2 categories  (Mono Support )

Viewed 8869 time(s), 2 post(s) 4/8/2012 9:37:30 AMby rule128
rule128

rule128

4/8/2012 9:37:30 AM
Hello

I am trying to get the groups that have the biggest number of members . In order to tdo that I can use the GetPopularGroups from the PopularListGroup in the samples folder.
Ineed to get those groups only from 2 specific categories. I have modified the GetPopularGroups as below.
The problem is that I do not get the desired effect if I call the method using the category slugs or ids.

What am i doing wrong?

Should this filter apply an "or" between the two categories? filter.PredicateExpression.AddWithOr(SnGroupFields.GroupCategoryId == new Guid(categoryId));

Regards

public List<SnGroupDTO> GetPopularGroups(string[] categorys, string[] categoryIds, 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 == 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 (categoryIds.Length >0)
{
foreach (string categoryId in categoryIds)
{
filter.PredicateExpression.AddWithOr(SnGroupFields.GroupCategoryId == new Guid(categoryId));
}
}
if (categorys.Length > 0)
{
foreach (string category in categorys)
{
filter.PredicateExpression.AddWithOr(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<SnGroupDTO> toReturn = groups.Select(group => new SnGroupDTO(group)).ToList<SnGroupDTO>();
return toReturn;
}
This content has not been rated yet. 
265 Reputation 29 Total posts
Goran

Goran

4/11/2012 8:06:39 AM

Hi!

Can you please describe what exactly happens when you run this modified code, what results do you get?

Also, can you double check that you are passing correct values for categorys and categoryIds?

Goran

This content has not been rated yet. 
206 Reputation 6 Total posts