Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine multiple statement in lambda expression

Tags:

c#

lambda

I am new to this LINQ field and one thing am trying to do.

I have an action delegate(written below) which i want to convert in lambda expression.

      Action<string> custom = delegate(string name)
            {
                lstCutomers.Add(new Customer(name, coutries[cnt]));
                name = name + " Object Created";
            };

What will be the lambda expression for same. I just want to know that can i write multiple statements in lambda if no then Why?

Thanks in advance.

like image 476
D J Avatar asked Jan 07 '12 08:01

D J


People also ask

Can lambda expressions contain multiple statements?

In particular, a lambda function has the following characteristics: It can only contain expressions and can't include statements in its body.

Can Python lambda have multiple lines?

No, you can't write multiline lambda in Python because the lambda functions can have only one expression. The creator of the Python programming language – Guido van Rossum, answered this question in one of his blogs. where he said that it's theoretically possible, but the solution is not a Pythonic way to do that.

How many expressions can lambda have?

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.


1 Answers

You can't create a lambda expression, since you're not returning anything. You can however create a statement lambda:

Action<string> custom = (name) =>
        {
            lstCutomers.Add(new Customer(name, coutries[cnt]));
            name = name + " Object Created";
        };
like image 160
George Duckett Avatar answered Oct 07 '22 20:10

George Duckett