Parallelizing some LINQ to XML

2019-06-09 06:54发布

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.
                   });
}

2条回答
Evening l夕情丶
2楼-- · 2019-06-09 07:31

How about this:

var list = (from i in ParallelEnumerable.Range(1, 10)
            from c in XDocument.Load(string.Format(url, i))
                               .Descendants("CropType")
            select new Crop
            {
                //The data here.
            }).ToList();

That's basically the same as Jeff M's code, but in query expression format.

Two things to note:

  • Your original code actually only produces 9 documents, not 10. Adjust my code to suit
  • The results will not necessarily be in the original order. If you need ordering, use the AsOrdered method.
查看更多
Viruses.
3楼-- · 2019-06-09 07:41

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:

var crops =
    ParallelEnumerable.Range(1, 9)
        .SelectMany(i =>
            XDocument.Load(string.Format(url, i))
                     .Descendants("CropType")
                     .Select(c => new Crop
                      {
                          // The data here
                      })
         )
        .ToList();
查看更多
登录 后发表回答