使用c#中的List优化代码

时间:2017-01-04 10:54:29

标签: c#

我有字符串列表可能包含大量记录,问题是我必须检查每条记录并收集为个别记录抛出的异常。我需要优化这个逻辑。下面是我的代码片段试过了。

private void testSearchTerms(List<string> searchTerms,List<string> failures,SearchDTSearch searchDTSearch,string index,int skipRecordsCount)
{
    if (searchTerms.Any())
    {
        try
        {
            using (SearchResults searchResults = searchDTSearch.Execute(index, searchTerms, null))
            {
                ;//do nothing, we don't care about the results, just the fact that there was an error or not
            }
        }
        catch (Exception ex)
        {
            if ((ex.Message != null) && (ex.Message.Contains("Operation failed: open (could not open directory)")))
            {
                LogManager.Warning("Attempt to test search term failed due to index access problem.  Path of index used to test: '" + index + "'", ex);
            }
            else
            {
                //collect individual search terms with error
                foreach (var searchTerm in searchTerms)
                {
                    try
                    {
                        using (SearchResults searchResults = searchDTSearch.Execute(index, searchTerm, null))
                        {
                            ;//do nothing, we don't care about the results, just the fact that there was an error or not
                        }
                    }
                    catch (Exception)
                    {
                        failures.Add(searchTerm);
                    }
                }
            }
        }
        finally
        {
            var dividedSearchTerms = searchTerms.Skip(skipRecordsCount).Take((searchTerms.Count/2)).ToList();
            skipRecordsCount += dividedSearchTerms.Count();
            this.testSearchTerms(dividedSearchTerms, failures, searchDTSearch, index, skipRecordsCount);
        }
    }
}

PS:不能更改Excute(),它需要List和string

PSS:不能在这里使用线程

0 个答案:

没有答案