0

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

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
根据 Magnus 的建议,我换掉了我QueryHandler.GetQueryResult的电话,ForumHandler.GetQueryResult但这似乎没有什么不同。我还削减了参数,GetQueryResult以便我的代码行现在是:

MessageCollection topics = ForumHandler.GetQueryResult(topicQuery);

但这仍然给了我一个 NullReferenceException。这表明问题不在于我正在调用的任何处理程序的 GetQueryResult 方法,而是在topicQuery对象中,因为这是迄今为止保持不变的东西。我想知道我是否缺少需要在对象上设置的TopicQuery属性。

2010 年 4 月 11 日更新
这里有需要考虑的“安全上下文”吗?即查询需要在可以完全访问所有论坛的用户的上下文中运行?

4

2 回答 2

1

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

于 2010-10-25T13:33:41.920 回答
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我从名称中推断出的允许您提交一组整数以在条件中的查询中使用(IN我猜是 SQL 子句)。

于 2010-12-09T14:53:03.537 回答