Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda Expression

Tags:

c#

lambda

Can I simplify this statement with a lambda expression?

var project = from a in accounts
              from ap in a.AccountProjects
              where ap.AccountProjectID == accountProjectId
              select ap;
like image 746
user194824 Avatar asked Oct 22 '09 18:10

user194824


People also ask

What is meant by lambda expression?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

Why is lambda used?

Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.

How is lambda () function called?

Lambda Function, also referred to as 'Anonymous function' is same as a regular python function but can be defined without a name. While normal functions are defined using the def keyword, anonymous functions are defined using the lambda keyword. However,they are restricted to single line of expression.


1 Answers

var project = accounts.SelectMany(a => a.AccountProjects)
                      .Where(x => x.AccountProjectID == accountProjectId);

Whether this is actually simpler is a matter of taste.

like image 133
Mark Avatar answered Oct 15 '22 06:10

Mark