C# Linq 问题

发布于 2024-10-26 20:03:45 字数 378 浏览 3 评论 0原文

LINQ 是否有一个序列运算符,它允许对每个元素执行某些操作,而无需将其投影到新序列?

这可能看起来有点尴尬,但只是让我知道:)

示例:

IEnumerable<IDisposable> x;
x.PERFORM_ACTION_ON_EVERY_ELEMENT(m => m.Dispose());

显然,这可以使用类似的东西来完成:

foreach (var element in x) x.Dispose();

但是如果某些东西确实存在,那就太好了。

Does LINQ have a sequence operator, which allows to perform some action on every element without projecting it to a new sequence?

This might see a bit awkward, but just for me to know :)

Example:

IEnumerable<IDisposable> x;
x.PERFORM_ACTION_ON_EVERY_ELEMENT(m => m.Dispose());

Obviously, this could be done using something like:

foreach (var element in x) x.Dispose();

But if something actually exists, that would be nice.

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

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

发布评论

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

评论(4

烟燃烟灭 2024-11-02 20:03:45

不,它不存在。具体来说,出于您提到的原因:拥有一个行为与所有其他操作员完全不同的操作员似乎很尴尬。

Eric Lippert,C# 编译器开发人员之一有一篇文章

但是我们可以在这里更深入一些。我在哲学上反对提供这样的方法,原因有两个。

第一个原因是这样做违反了所有其他序列运算符所基于的函数式编程原则。显然,调用此方法的唯一目的是产生副作用。

表达式的目的是计算值,而不是产生副作用。语句的目的是产生副作用。这个东西的调用点看起来非常像一个表达式(不过,不可否认,由于该方法是返回 void 的,所以该表达式只能在“语句表达式”上下文中使用。)

创建唯一一个仅对其副作用有用的序列运算符并不适合我。

No, it doesn't exist. Specifically for the reason you mention: It seems awkward having a single operator that behaves completely different than all the others.

Eric Lippert, one of the C# Compiler developers has an article about this.

But we can go a bit deeper here. I am philosophically opposed to providing such a method, for two reasons.

The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects.

The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect. The call site of this thing would look an awful lot like an expression (though, admittedly, since the method is void-returning, the expression could only be used in a “statement expression” context.)

It does not sit well with me to make the one and only sequence operator that is only useful for its side effects.

勿忘心安 2024-11-02 20:03:45

您可以使用此方法:

public static class Extension
{
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var t in source)
        {
            action(t);
        }
        return source;
    }
}

它返回源,以便您可以根据需要将其传递给另一个扩展方法。或者如果你想无效,你可以稍微改变一下方法。

You can use this method:

public static class Extension
{
    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var t in source)
        {
            action(t);
        }
        return source;
    }
}

It returns the source so you can pass it along to another extension method as needed. Or if you want to be void, you can change the method a little bit.

无边思念无边月 2024-11-02 20:03:45

morelinq 项目有一个 ForEach 运算符。 LINQ 本身没有,因为 LINQ 完全是关于函数式编程的,而 ForEach 有副作用。

The morelinq project has a ForEach operator. LINQ itself doesn't, as LINQ is all about functional programming, and ForEach has side effects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文