0

我正在尝试从 SharePoint 2010 中检索同义词,并且我的代码存在以下问题:它循环通过keywordCollection没有索引器的 a。执行 aforeach耗时太长,因为实例化一个新关键字大约持续 5-10 毫秒,到目前为止大约有 8000 个关键字,大约需要 80 秒才能完成。到目前为止我尝试过的事情:

  • 获取枚举器 -> 也需要 80 秒

  • 将集合转换为列表->由于未知原因而失败。

代码示例:

KeywordContext keywordContext = fastProxy.KeywordContext;
SearchSettingGroupCollection searchSettingGroupCollection = keywordContext.SearchSettingGroups;

foreach (SearchSettingGroup searchSettingGroup in searchSettingGroupCollection)
{
    if (searchSettingGroup.Name == siteId.ToString())
    {
        foreach (Keyword keyword in searchSettingGroup.Keywords)
        {
            //the rest of the work here, per total takes about 470ms
        }
    }
}

有没有一种方法可以在不使用语句的情况下循环遍历集合,该语句每次都foreach实例化一个新的?object<T>

谢谢!

4

1 回答 1

0

尝试这个:

using (var site = new SPSite(siteCollectionUrl))
{
    // get the site ID
    var guid = site.ID.ToString();

    // code to get the fastProxy FASTAdminProxy

    KeywordContext keywordContext = fastProxy.KeywordContext;
    var ssgs = keywordContext.SearchSettingGroups;
    if (ssgs.ContainsSearchSettingGroup(guid))
    {
        var searchSettingGroup = ssgs.GetSearchSettingGroup(guid);
        foreach (var keyWord in searchSettingGroup.Keywords)
        {
            // do stuff with keyWord.Synonyms
        }
    }
}

另外,虽然我目前无法测试它,但您可以尝试使用 Parallel该类:

using System.Threading.Tasks;

// ...

Keyword[] array = new Keyword[searchSettingGroup.Keywords.Count];
searchSettingGroup.Keywords.CopyTo(array, 0);
Parallel.ForEach<Keyword>(array, keyword =>
{
    //do stuff here
});
于 2013-02-14T12:05:27.553 回答