获取用户俱乐部中的论坛的最新主题和回复时的NullReferenceException

时间:2010-10-25 13:04:07

标签: episerver episerver-6

我正在尝试查询用户俱乐部的论坛集(论坛?),以获取这些论坛中的最新主题和回复。这是我正在使用的代码:

RoomCollectionCriterion userClubRoomsCollection = new RoomCollectionCriterion();

foreach (Club userClub in userClubCollection)
{
    RoomCriterion userClubRoomCriterion = new RoomCriterion();
    userClubRoomCriterion.ID = new IntegerCriterion();
    userClubRoomCriterion.ID.Value = userClub.ForumRoom.ID;

    userClubRoomsCollection.Criteria.Add(userClub.Name, userClubRoomCriterion);
}

TopicQuery topicQuery = new TopicQuery();

topicQuery.Room = new RoomCriterion();
// Pass in our collection of the user's clubs to the query
topicQuery.Room.Rooms = userClubRoomsCollection;

MessageCollection topics = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, pageNumber, 2);
MessageCollection replies = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, pageNumber, 4);

我遇到的问题是每次第一次拨打GetQueryResult时都会收到NullReferenceException。我在传递给GetQueryResult的内容时看不出任何错误/遗漏,但我希望其他人之前已经做过这样的事情,并且可以建议解决这个问题。

更新26/10/2010
正如马格努斯所建议的,我换了QueryHandler.GetQueryResult来拨打ForumHandler.GetQueryResult,但这似乎没有什么区别。我还将参数调整为GetQueryResult,以便我的代码行现在是:

MessageCollection topics = ForumHandler.GetQueryResult(topicQuery);

但这仍然给我一个NullReferenceException。这对我来说问题不在GetQueryResult方法中,无论我在哪个处理程序中调用它,它都在topicQuery对象中,因为到目前为止这个问题一直保持不变。我想知道我是否缺少需要在TopicQuery对象上设置的属性。

2010年4月11日更新
是否存在需要考虑的“安全背景”?即查询需要在对所有论坛具有完全访问权限的用户的上下文中运行吗?

2 个答案:

答案 0 :(得分:1)

尝试使用EPiServer.Community.Forum.ForumHandler.GetQueryResult()及其重载。

答案 1 :(得分:0)

对此的解决方案(由我的同事发现)是:

TopicQuery topicQuery = new TopicQuery();

//For latest topics we want to sort by the topics creation date.
topicQuery.CreateDate = new DateTimeCriterion();
topicQuery.Room = new RoomCriterion();
topicQuery.Room.ID = new IntegerCriterion();
//We want to include several roomIDs
topicQuery.Room.ID.Includes = new IntegerInCriterion();

//For each club, get its ForumRoom
foreach (Club userClub in userClubCollection)
{
    topicQuery.Room.ID.Includes.Values.Add(userClub.ForumRoom.ID);
}

//Sorting of results
CriterionSortOrder critSort = new CriterionSortOrder(topicQuery.CreateDate, EPiServer.Common.Sorting.SortingDirection.Descending);

//Add sorting to query
topicQuery.OrderBy.Add(critSort);

MessageCollection topics = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, 1, 2);
topicQuery.OrderBy.Remove(critSort);

//For Latest replies we want to sort topics by LastReply.CreateDate. Therefore we add this. 
topicQuery.LastReply = new ReplyCriterion();
topicQuery.Replies = new ReplyCollectionCriterion();
topicQuery.Replies.Count = new IntegerCriterion();
topicQuery.Replies.Count.Operator = ComparisonOperator.GreaterThan | ComparisonOperator.Equals;
topicQuery.Replies.Count.Value = 1;
topicQuery.LastReply.CreateDate = new DateTimeCriterion();
CriterionSortOrder critSort2 = new CriterionSortOrder(topicQuery.LastReply.CreateDate, EPiServer.Common.Sorting.SortingDirection.Descending);

topicQuery.OrderBy.Add(critSort2);
MessageCollection replies = QueryHandler.GetQueryResult<EPiServer.Community.Forum.Topic, MessageCollection>(topicQuery, 1, 4);

lvMyEntries.DataSource = topics;
lvMyEntries.DataBind();
lvMyReplies.DataSource = replies;
lvMyReplies.DataBind();

这里的关键点似乎是使用我在名称中推断的IntegerInCriterion允许您在标准中提交一组要在查询中使用的整数(如SQL {{1}我猜是。)