How can I make this code run in parallel?
List<Crop> crops = new List<Crop>();
//Get up to 10 pages of data.
for (int i = 1; i < 10; i++)
{
//i is basically used for paging.
XDocument document = XDocument.Load(string.Format(url, i));
crops.AddRange(from c in document.Descendants("CropType")
select new Crop
{
//The data here.
});
}
How about this:
That's basically the same as Jeff M's code, but in query expression format.
Two things to note:
AsOrdered
method.I don't know why you'd want to parallelize that, I suspect the overheads will bog your program down since you aren't doing anything really significant. But for a translation, I suppose this would do. I don't know what you want parallel so assuming you want the loop to run in parallel: