第26章 计算限制的异步操作(11)

本文介绍如何使用并行语言集成查询(PLINQ)处理大量数据,包括将顺序查询转换为并行查询的方法,并展示了一个具体的并行查询示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

26.7 并行语言集成查询(PLINQ)

如果同时要处理大量项,或者每一项的处理过程都是一个耗时的计算限制的操作,那么可以从并行LINQ获得最大的利益。

静态System.Linq.ParallelEnumerable类(在System.Core.dll中定义)实现了PLINQ的所有功能。

为了让自己的Linq to Objects查询调用这些方法的并行版本,必须将自己的顺序查询(基于IEnumerable或者IEnumerable<T>)转换成并行查询(基于ParallelQuery或者ParallelQuery<T>),这是用ParallelEnumerable的AsParallel扩展方法来实现的。

using System;
using System.Linq;
using System.Reflection;

namespace ThreadStudy
{
    public class Program
    {
        static void Main(string[] args)
        {
            Assembly ass = Assembly.LoadFrom( @"C:\Documents and Settings\10170660\Desktop\RdfBrowser(certificate)\RdfBrowser(certificate)\OpenLink.Data.Virtuoso.dll");
            ObsoleteMethods(ass);
            Console.ReadKey();
        }

        private static void ObsoleteMethods(Assembly assembly)
        {
            var query=from type in assembly.GetExportedTypes().AsParallel() //转换成并行查询
                      from method in type.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static)
                      let obsoleteAttrType=typeof(ObsoleteAttribute)
                      where Attribute.IsDefined(method,obsoleteAttrType)
                      orderby type.FullName
                      let obsoleteAttrObj=(ObsoleteAttribute)Attribute.GetCustomAttribute(method,obsoleteAttrType)
                      select string.Format("Type={0}\nMethod={1}\nMessage={2}\n",type.FullName,method.ToString(),obsoleteAttrObj.Message);
            foreach (var result in query) Console.WriteLine(result);
        }
    }
}
Output:

Type=OpenLink.Data.Virtuoso.SqlExtendedStringType
Method=System.String ToString(System.IFormatProvider)
Message=The provider argument is not used. Please use ToString().

Type=OpenLink.Data.Virtuoso.SqlExtendedStringType
Method=System.String ToString(System.String, System.IFormatProvider)
Message=The provider argument is not used. Please use ToString(String).

Type=OpenLink.Data.Virtuoso.VirtDbType
Method=System.String ToString(System.String, System.IFormatProvider)
Message=The provider argument is not used. Please use ToString(String).

Type=OpenLink.Data.Virtuoso.VirtDbType
Method=System.String ToString(System.IFormatProvider)
Message=The provider argument is not used. Please use ToString().

Type=OpenLink.Data.Virtuoso.VirtuosoPermission
Method=Void Deny()
Message=Deny is obsolete and will be removed in a future release of the .NET Fra
mework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
虽然不太常见,但在一个查询中,可以从执行并行操作切换回顺序操作,这是通过调用ParallelEnumerable的AsSequential方法做到的。

如果希望以并行方式处理查询结果,就应该使用ParallelEnumerable的ForAll方法处理查询。

            query.ForAll(res => Console.WriteLine(res));
如果要让PLinq保持数据项的顺序,可调用ParallelEnumerable的AsOrdered方法。

AsParallel().AsOrdered()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值