现有类似的Parallel.For LINQ扩展方法? [英] Existing LINQ extension method similar to Parallel.For?

查看:334
本文介绍了现有类似的Parallel.For LINQ扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
相当于LINQ的foreach为IEnumerable的< T>

的IEnumerable的LINQ的扩展方法是非常方便......但没有多大用处的,如果你想要做的是应用一些计算来枚举每个项目不返回任何东西。所以我在想,如果可能我只是缺少正确的方法,或者如果它确实不存在,因为我宁愿使用一个内置的版本,如果它的可用...但我还没有找到一个: - )

The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in version if it's available ... but I haven't found one :-)

我可以发誓有一个.ForEach方法的地方,但我还没有找到它。在此期间,我确实写的情况下,我自己的版本是非常有用的其他人:

I could have sworn there was a .ForEach method somewhere, but I have yet to find it. In the meantime, I did write my own version in case it's useful for anyone else:

using System.Collections;
using System.Collections.Generic;

public delegate void Function<T>(T item);
public delegate void Function(object item);

public static class EnumerableExtensions
{
    public static void For(this IEnumerable enumerable, Function func)
    {
        foreach (object item in enumerable)
        {
            func(item);
        }
    }

    public static void For<T>(this IEnumerable<T> enumerable, Function<T> func)
    {
        foreach (T item in enumerable)
        {
            func(item);
        }
    }
}



用法是:

usage is:

myEnumerable.For< MyClass的>(委托(MyClass的项目){item.Count ++;});

推荐答案

脱落就多一分光线原因:

Shedding a little more light on why:

LINQ是功能性的。它用于查询数据和返回结果。 LINQ查询不应该改变应用程序(像缓存一些例外)的状态。因为的foreach不会返回任何结果,它不会有很多用途,不涉及改变的东西,除了国家要传递什么吧。如果你需要一个foreach()扩展方法,它容易推出自己的。

LINQ is functional in nature. It is used to query data and return results. A LINQ query shouldn't be altering the state of the application (with some exceptions like caching). Because foreach doesn't return any results, it doesn't have many uses that don't involve altering the state of something besides what you are passing in to it. And if you need a Foreach() extension method, it is easy to roll your own.

如果,在另一方面,你要的是采取输入并调用返回结果的每一项功能,LINQ通过其选择的方法的一种方式。

If, on the other hand, what you want is to take input and call a function on each item that returns a result, LINQ provides a way through its select method.

例如,下面的代码调用一个函数委托对列表中的每一个项目,返回true,如果该项目是积极的:

For example, the following code calls a function delegate on every item in a list, returning true if that item is positive:

    static void Main(string[] args)
    {
        IEnumerable<int> list = new List<int>() { -5, 3, -2, 1, 2, -7 };
        IEnumerable<bool> isPositiveList = list.Select<int, bool>(i => i > 0);

        foreach (bool isPositive in isPositiveList)
        {
            Console.WriteLine(isPositive);
        }

        Console.ReadKey();        
    }

这篇关于现有类似的Parallel.For LINQ扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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