Try our conversational search powered by Generative AI!

How to get EntryComments Sorted on Email

Vote:
 

Hi, 

I want to collect BlogEntries-comments. I want to use paging and sorting in order to display them correctly. 

Paging works fine. And sorting on Created also works. But apparently, the EntryCommentQuery does not support ordering on Email and/or Status? I can order the comments with LINQ after I got a subset of comments, but then the ordering isn't working correctly. 

So, how can I get the comments, sorted on email / status? Here is my code so far: 

 

var query = new EntryCommentQuery();
switch (sortField)
{
    case "status":
        //query.OrderBy.Add(new CriterionSortOrder(query.Created, sortDirection));
        break;
    case "email":
        //query.OrderBy.Add(new CriterionSortOrder(query.Created, sortDirection));
        break;
    case "created":
    default:
        query.OrderBy.Add(new CriterionSortOrder(query.Created, sortDirection));
        break;
}
var col = QueryHandler.Instance.GetQueryResult<EntryComment, EntryCommentCollection>
    (query, pageNr, pageSize, out totalItems)
    .Select(c => new CommunityComment(c));

    

 Best, Leonard

#66421
Feb 28, 2013 11:29
Vote:
 

I haven't found a solution (not sure whether it's possible to get comments sorted by email/status), but I did found a work-around: get all the comments, then filter out the comments you don't want, and then order the List of comments. Here is the code: 

 

var query = new EntryCommentQuery();
var col = QueryHandler.Instance.GetQueryResult<EntryComment, EntryCommentCollection>(query)
                .Select(c => new CommunityComment(c));

// filter for the current site's languages
var comments = col.Where(
	c => (languageBranch == null || languageBranch == c.LanguageBranch) &&
		 (startDate == null || c.Created > startDate) &&
		 (
			(getApproved && c.Status == CommunityCommentStatus.Approved) ||
			(getAbuse && c.Status == CommunityCommentStatus.AbuseReported) ||
			(getWaiting && c.Status == CommunityCommentStatus.WaitingApproval) ||
			(getSpamSuspected && c.Status == CommunityCommentStatus.SpamSuspected) ||
			(getMatchPhrases && new CommentPhrasesMatcher(c.Content).Match(false))
		 )
	);
	
int sortDirection = (int)(ViewState["SortDirection"] ?? 1);
string sortField = (string)ViewState["SortField"] ?? "Created";
switch (sortField)
{
	case "Status":
		comments = comments.OrderBy(c => sortDirection * (int)c.Status).ToList();
		break;
	case "Email":
		comments = comments.OrderBy(c => c.Author, new AuthorEmailComparer(sortDirection)).ToList();
		break;
	case "Created":
	default:
		comments = comments.OrderBy(c => sortDirection * -c.Created.Ticks).ToList();
		break;
}

    

#71770
May 28, 2013 17:05
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions 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.