Get Club members sorted and paged

Vote:
 

Hi,

I'm using this 2 lines of code to get Club approved members sorted by users names:

MembershipSortOrder sortOrder = new MembershipSortOrder(MembershipSortField.ClubName, EPiServer.Common.Sorting.SortingDirection.Ascending); 
MembershipCollection members = ClubHandler.Instance.GetMembers(club, MembershipApproval.Approved, pagerIndex, pageSize, out totalItems, sortOrder);
      

This is working fine, but now I need to get only members with specific attributes.

I tried this code:


MembershipQuery query = new MembershipQuery();
query.Club = new ClubCriterion();
query.Club.ID = new EPiServer.Common.Queries.IntegerCriterion() { Value = club.ID, Operator = EPiServer.Common.Queries.ComparisonOperator.Equals };
query.MembershipType = new MembershipTypeCriterion();
query.MembershipType.Value = MembershipType.Member;
query.User = new UserCriterion();
query.User["AttributeName"] = new StringCriterion() { Value = "AttributeValue" };
MembershipCollection memberships = ClubHandler.Instance.GetQueryResult(query);

but I can't make it sorted and the pagging is lost. Using :

query.User.OrderBy.Add(new CriterionSortOrder(query.User.GivenName, EPiServer.Common.Sorting.SortingDirection.Ascending));

thrown an error - "The criterion 'GivenName' does not represent a database column, and cannot be sorted on"

Is there any way to overcome this or using LINQ is the only solution. Something like this:

var membershipsPaged = memberships.OrderBy(b => b.User.SurName).Skip(pagerIndex * pageSize).Take(pageSize);

Thank you in advance.

#57755
Mar 26, 2012 14:57
Vote:
 

Hi, 

The GetQueryResult method contains an overload that accepts paging: 

ClubHandler.Instance.GetQueryResult(query, pagerIndex, pageSize, out totalItems);

When it comes to the sorting, from what I can see in the GetOrderByQuery() method in EPiServer.Common.Queries.QueryBase you can only sort on a property that exists in your query object. So in your case, you can only sort on the properties of the MembershipQuery class: 

// In the GetOrderByQuery() method  
foreach (CriterionSortOrder order in this.OrderBy)
    {
        CriterionBase criterion = (CriterionBase) order.Criterion;
        string name = criterion.ParentQuery.CriteriaReverse[criterion];
        if (base.GetType().GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) == null)
        {
            throw new ApplicationException(string.Format("The criterion '{0}' does not represent a database column, and cannot be sorted on.", name, criterion.GetType().ToString()));
        }
        if (order != this.OrderBy[0])
        {
            builder.Append(", ");
        }
        builder.Append(order.ToString());
    }

    

If you take a look at the code above, you see that base.GetType().GetProperty(...) returns null in your case because MembershipQuery does not contain a GivenName property. 

I've tried to figure out if there's any way to get around this, but sadly I couldn't find any other way than the LINQ solution you also came up with. 

I hope this helped you understand the error message, sorry I couldn't find a solution!

Karoline

#57770
Mar 27, 2012 12:14
Vote:
 

Hi BoyanNikolov,

I am now getting the same issue with you.

If you have got through this, can you specify your way to achieve it?

And please can you tell me how can I take just users only.

Thanks!

#85789
May 05, 2014 20:45
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.