一些并行的LINQ to XML [英] Parallelizing some LINQ to XML

查看:166
本文介绍了一些并行的LINQ to XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使并行的代码运行

 列表<农作物>农作物=新名单<农作物>(); 
//获取多达10页的数据。
的for(int i = 1;我小于10;我++)
{
//我基本上是用于分页。
的XDocument文档= XDocument.Load(的String.Format(URL,I));在document.Descendants

crops.AddRange(从C(CropType)
选择新作物
{
//这里的数据。
} );
}


解决方案

我不知道为什么你想要并行的是,我怀疑费用就会陷入你的程序下来,因为你没有做任何事情真的显著。但是,对于一个翻译,我想这会做。我不知道你要平行地假设你希望循环并行运行什么:

  VAR作物= 
ParallelEnumerable.Range(1,9)
.SelectMany(ⅰ= GT;
XDocument.Load(的String.Format(网址,i))的
.Descendants(CropType)
。选择(C =>新建作物
{
//数据在这里
})

.ToList();


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

解决方案

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();

这篇关于一些并行的LINQ to XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆