I've got a method:
IList<string> pages = new List<string>();
foreach (var node in nodes)
{
try
{
string temp = DoSomeComplicatedModificationOnNode(node);
if (temp.ToLower().Contains(path))
{
pages.Add(node.Title);
}
}
catch (Exception)
{
continue;
}
}
DoSomeComplicatedModificationOnNode() gives exception in some cases, that's why the try{} catch block is used - I can skip the items which gives exception. The number of nodes contains several thousands of items, an item has several properties. How can I optimize this loop? I was thinking about Parallel.Foreach, but the following code gives me an error "Missing current principal":
IList<string> pages = new List<string>();
Parallel.ForEach(pageNodes, node =>
{
try
{
string temp = DoSomeComplicatedModificationOnNode(node);
if (temp.ToLower().Contains(path))
{
pages.Add(node.Title);
}
}
catch (Exception)
{
}
});
exceptionisn't a great idea , you should catch the correct exceptions.ToLowerInvariant, notToLower.