LINQ:如何根据上下文修改 AsParallel 的返回类型?

发布于 2024-12-14 04:58:05 字数 801 浏览 1 评论 0原文

LINQ 的 AsParallel 返回 ParallelQuery。我想知道是否可以更改此行为,以便我可以比较使用并行性和不使用并行性运行的 LINQ 语句,而无需实际更改代码?此行为应该类似于 Debug.Assert - 当未设置 DEBUG 预处理器指令时,它会被优化。因此,我希望能够使 AsParallel 返回相同的类型,而无需将其转换为 ParallelQuery

我想我可以声明我自己的扩展方法(因为我无法重写 AsParallel)并在其中分析预处理器指令:

    public static class MyExtensions
    {
#if TURN_OFF_LINQ_PARALLELISM
        public static IEnumerable<T> AsControllableParallel<T>(this IEnumerable<T> enumerable)
        {
            return enumerable;
        }
#else
        public static ParallelQuery<T> AsControllableParallel<T>(this IEnumerable<T> enumerable)
        {
            return enumerable.AsParallel();
        }
#endif
    }

我想知道是否还有其他方法。我的要求是不是太多了?

LINQ's AsParallel returns ParallelQuery. I wonder if it's possible to change this behavior so that I could compare the LINQ statement run with and without parallelism without actually changing the code? This behavior should be similar to Debug.Assert - when DEBUG preprocessor directive is not set, it's optimized out. So I'd like to be able to make AsParallel return the same type without converting it to ParallelQuery.

I suppose I can declare my own extension method (because I can't override AsParallel) and have that preprocessor directive analyzed within it:

    public static class MyExtensions
    {
#if TURN_OFF_LINQ_PARALLELISM
        public static IEnumerable<T> AsControllableParallel<T>(this IEnumerable<T> enumerable)
        {
            return enumerable;
        }
#else
        public static ParallelQuery<T> AsControllableParallel<T>(this IEnumerable<T> enumerable)
        {
            return enumerable.AsParallel();
        }
#endif
    }

I wonder if there is any other way. Am I asking for too much?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-12-21 04:58:05

怎么样

            var result = 
                source
#if TURN_ON_LINQ_PARALLELISM
                .AsParallel()
#endif
                .Select(value => value.StartsWith("abcd"));

What about

            var result = 
                source
#if TURN_ON_LINQ_PARALLELISM
                .AsParallel()
#endif
                .Select(value => value.StartsWith("abcd"));
灯下孤影 2024-12-21 04:58:05

您可以创建自己的扩展方法,并将这些方法转发到发布版本中的“真正的”AsParallel 方法,并在调试版本中使用顺序方法。通过检查当前命名空间中的方法来解析扩展方法,然后搜索“外部”命名空间(仍然首选实例方法)。

   class NonParallelQuery 
    {
        IEnumerable _data;
        public NonParallelQuery(IEnumerable data)
        {
            _data = data;

        }
        public IEnumerator GetEnumerator()
        {
            return _data.GetEnumerator();
        }
    }

    static class Extensions
    {
        public static NonParallelQuery AsParallel(this IEnumerable source)
        {
#if DEBUG
            return new NonParallelQuery(ParallelEnumerable.AsParallel(source));
#else
            return new NonParallelQuery(source);
#endif
        }
    }


    public partial class Form1 : Form
    {

        public Form1()
        {
            var data = new string[] { "first", "second" };
            foreach (var str in data.Select(x => x.ToString()).AsParallel())
            {
                Debug.Print("Value {0}", str);
            }
            InitializeComponent();
        }

You can create your own extension methods and forward these to the "real" AsParallel method in release builds and use the sequential ones in debug builds. Extension methods are resolved by checking for method in the current namespace and then are the "outer" namespaces searched (instance methods are still preferred).

   class NonParallelQuery 
    {
        IEnumerable _data;
        public NonParallelQuery(IEnumerable data)
        {
            _data = data;

        }
        public IEnumerator GetEnumerator()
        {
            return _data.GetEnumerator();
        }
    }

    static class Extensions
    {
        public static NonParallelQuery AsParallel(this IEnumerable source)
        {
#if DEBUG
            return new NonParallelQuery(ParallelEnumerable.AsParallel(source));
#else
            return new NonParallelQuery(source);
#endif
        }
    }


    public partial class Form1 : Form
    {

        public Form1()
        {
            var data = new string[] { "first", "second" };
            foreach (var str in data.Select(x => x.ToString()).AsParallel())
            {
                Debug.Print("Value {0}", str);
            }
            InitializeComponent();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文