串联lambda函数在C# [英] Concatenating Lambda Functions in C#

查看:175
本文介绍了串联lambda函数在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#3.5我想建立一个谓词一块发送给where子句一块。我创建了一个非常简单的控制台应用程序来说明我到达的解决方案。这完美的作品。万无一失。但我不知道如何或为何

Using C# 3.5 I wanted to build up a predicate to send to a where clause piece by piece. I have created a very simple Console Application to illustrate the solution that I arrived at. This works perfectly. Absolutely perfectly. But I have NO idea how or why.

    public static Func<Tran, bool> GetPredicate()
    {
        Func<Tran, bool> predicate = null;
        predicate += t => t.Response == "00";
        predicate += t => t.Amount < 100;
        return predicate;
    }

当我说'谓语+ =',这是什么意思?谓词 - =似乎什么也不做,^ =,&安培; =,* =,/ =不是由编译器喜欢

When I say 'predicate +=', what does that mean? predicate -= appears to do nothing and ^=, &=, *=, /= aren't liked by the compiler.

编译器不喜欢'。谓语=谓语+ T => t.Response ......无论是。

The compiler doesn't like 'predicate = predicate + t => t.Response....' either.

有什么我无意中发现了?我知道它做什么,但它是如何做到的?

What have I stumbled on? I know what it does, but how does it do it?

如果有人愿意去钻研更复杂的lambda的,请这样做。

If anyone wants to go delve into more complicated lambda's, please do so.

推荐答案

委托+ =方法是运营商播委托相结合的方法来委派。
在另一方面委托 - =法是操作员从委托删除方法。
它是有用的行动

"delegate += method" is operator for multicast delegate to combine method to delegate. In the other hand "delegate -= method" is operator to remove method from delegate. It is useful for Action.

Action action = Method1;
action += Method2;
action += Method3;
action -= Method2;
action();

在这种情况下,唯一的方法1和Method3运行,方法2将无法运行,因为你之前删除方法调用该委托。

In this case, only Method1 and Method3 will run, Method2 will not run because you remove the method before invoke the delegate.

如果您使用多播委托与FUNC按键,结果将是最后的方法。

If you use multicast delegate with Func, the result will be last method.

Func<int> func = () => 1;
func += () => 2;
func += () => 3;
int result = func();

在这种情况下,结果将是3,由于方法()=> 3是最后的方法添加到委托。反正所有的方法将被称为

In this case, the result will be 3, since method "() => 3" is the last method added to delegate. Anyway all method will be called.

在你的情况,方法T => t.Amount< 100将有效

In your case, method "t => t.Amount < 100" will be effective.

如果你想谓词结合,我建议这些扩展方法

If you want to combine predicate, I suggest these extension methods.

public static Func<T, bool> AndAlso<T>(
    this Func<T, bool> predicate1, 
    Func<T, bool> predicate2) 
{
    return arg => predicate1(arg) && predicate2(arg);
}

public static Func<T, bool> OrElse<T>(
    this Func<T, bool> predicate1, 
    Func<T, bool> predicate2) 
{
    return arg => predicate1(arg) || predicate2(arg);
}



用法

Usage

public static Func<Tran, bool> GetPredicate() {
    Func<Tran, bool> predicate = null;
    predicate = t => t.Response == "00";
    predicate = predicate.AndAlso(t => t.Amount < 100);
    return predicate;
}



编辑:纠正扩展方法名称作为基思建议

correct the name of extension methods as Keith suggest.

这篇关于串联lambda函数在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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