Sorting forum topic queryresult

Vote:
 

What i need to do is create  a query that will get the last 5 updates in a forum room regardless if it is a new post or a reply to an old post.

 

To order on new replies i would add something like this:

topicQuery.OrderBy.Add(new CriterionSortOrder(topicQuery.LastReply.Created, SortingDirection.Descending));   

And to order on new topics i would add something like this:

topicQuery.OrderBy.Add(new CriterionSortOrder(topicQuery.Created, SortingDirection.Descending));

    

This dosent do what i want though :(

What i want is for it to order by LastReply.Created where there is a reply and on Created if the Topic dosent have any replies.

 

How would i go about ordering my results in that way?

#51183
May 26, 2011 15:44
Vote:
 

Have you tried using ForumHandler.GetTopics? This is what I use and it seems to produce the result I expect:

ForumHandler.Instance.GetTopics(rooms, EntityStatus.Approved, page, pageSize, out itemCount, new TopicSortOrder(TopicSortField.LastReply, SortingDirection.Descending)); 

(rooms is a RoomCollection but it can of course contain only one room)

#51190
May 27, 2011 7:41
Vote:
 

Yes, i have tried GetTopics but it behaved a bit wierd where a few Topics ended up at the end of the list eventhough hey where newer then some of the others.

GetTopics also puts the prioritized Topics at the top, something that i dont want in all cases so a custom query seemed to be the way to go.

#51232
May 30, 2011 9:46
Vote:
 

I had the same problem and after tearing my hair for hours I created a new class called CommentObject.

************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Extra.classes
{
public class CommentObject
{
private string _title;
private string _author;
private string _authorUrl;
private DateTime _commentDate;
private string _commentUrl;
private string _typeOfComment;
private int _nrOfComments;
public string Title
{
get {return _title;}
set{_title = value;}
}
public string Author
{
get { return _author; }
set { _author = value; }
}
public string AuthorUrl
{
get { return _authorUrl; }
set { _authorUrl = value; }
}
public string CommentUrl
{
get { return _commentUrl; }
set { _commentUrl = value; }
}
public DateTime CommentDate
{
get { return _commentDate; }
set { _commentDate = value; }
}
public string TypeOfComment
{
get { return _typeOfComment; }
set { _typeOfComment = value; }
}
public int NrOfComments
{
get { return _nrOfComments; }
set { _nrOfComments = value; }
}
}

// Custom comparer for the Product class
class CommentObjectComparer : IEqualityComparer<CommentObject>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(CommentObject x, CommentObject y)
{

//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;

//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;

//Check whether the products' properties are equal.
return x.Title == y.Title && x.CommentDate == y.CommentDate;
}

// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.

public int GetHashCode(CommentObject comment)
{
//Check whether the object is null
if (Object.ReferenceEquals(comment, null)) return 0;

//Get hash code for the Title field if it is not null.
int hashCommentTitle = comment.Title == null ? 0 : comment.Title.GetHashCode();

//Get hash code for the CommentDate field.
int hashCommentDate = comment.CommentDate.GetHashCode();

//Calculate the hash code for the CommentObject.
return hashCommentTitle ^ hashCommentDate;
}
}
}

*****************************************************************

I then made 2 queries. One to get the latest replies (ReplyQuery) and one for the latest topics (TopicQuery) and for each collection I did;

IList<CommentObject> commentholder = new List<CommentObject>();

foreach (Reply reply in replies)
{
       CommentObject commentObject = new CommentObject();
       commentObject.Title = reply.Subject;
       commentObject.Author = reply.Author.Name;
       commentObject.AuthorUrl = ((UserAuthor)reply.Author).User.GetMyPageUri().ToString();
       commentObject.CommentDate = reply.CreateDate;
       commentObject.CommentUrl = reply.Topic.GetTopicUri().ToString();
       commentholder.Add(commentObject);
}
foreach Topic t in topics) {
        ...same as above..
}

IList<CommentObject> sortedList = commentholder.OrderByDescending(o => o.CommentDate).ToList<CommentObject>();

Then I Databound the sortedList.

and rendered it in a ListView like;

<asp:HyperLink ID="hlEntryTitle" runat="server" Text="<%#((Extra.classes.CommentObject)Container.DataItem).Title %>" NavigateUrl="<%#((Extra.classes.CommentObject)Container.DataItem).CommentUrl %>" CssClass="large"/>

Surely there must be an easier way but I could not find it. If you figure something out, please let me know! :-D

/Jens

#53993
Sep 28, 2011 14:43
Vote:
 

Did you find a solution?

#64478
Dec 21, 2012 10:56
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.