Lambda Expression Syntax

Is it mandatory to use a lambda expression when using LINQ, or is a lambda expression optional?

Lambda expressions always use the => sign. What does it mean?

  customers.Where(c => c.City == "London"); 

Here c => , but why? What is the use of c => . Please discuss in detail, because I do not know lambda.

+7
source share
7 answers

Lambda expression

 c => c.City == "London" 

is a shorthand for something like

 bool IsCustomerInLondon(Customer c) { return (c.City == "London"); } 

This is a very simple way to write a simple function that returns a value. It was called an “anonymous function” because it never assigned a name or a formal definition (parameter types and return type are inferred from the context).

(Actually, this is not just a shorthand, lambda expressions are associated with some other constructs called closures , which are very cool and powerful tools.)

+7
source

No, you do not need to use a lambda expression. For example, your Where example could be written as:

 private static bool IsLondon(Customer customer) { return customer.City == "London"; } ... var londoners = customers.Where(IsLondon); 

This assumes LINQ for objects, of course. For LINQ to SQL, etc. You need to build an expression tree.

As for why “=>” is always used in a lambda expression, it's just because this is how the statement is written — it's like asking why “+” is used to add.

The lambda expression "c => ..." effectively gives a lambda expression a parameter called c ... in this case, a generic type inference provides type c . The body provides either an action to execute or some calculation to return a value based on c .

A full description of lambda expressions is beyond the scope of this answer. As a blatant jam for my book , however, they are described in detail in chapter 9 of C # in depth.

+11
source

Think of lambdas as anonymous functions. I will try to explain this with the following code.

 public bool IsLondon(Customer customer) { return customer.City == "London"; } 

Now we discard the function name and get rid of curly braces:

 public bool Customer customer return customer.City == "London"; 

We do not need a return type, because the compiler can infer the type from the expression:

  customer.City == "London"; 

In the same way, the compiler knows about the type of input, so we do not need to specify it.

So basically we left

 customer return customer.City == "London"; 

And the lambda syntax in C # is basically:

 (parameter list) => "expression" 

You can also write multi-line expressions, but then you should surround your code with curly braces. You will also need to use the "return" operator, as usual.

+4
source

Lambda and linq are pretty separate. You can use it without using another (there are parts of linq that depend on lambda expressions, but we want to keep it simple :-))

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

This was from MSDN. (http://msdn.microsoft.com/en-us/library/bb397687.aspx)

To make it short (this is much more complicated), you can use the lambda expression to create a local function. what you put before => (in your example c) will be the parameter of the function. The return type is "discovered" by the C # compiler.

c => c.City == "London" almost equivalent:

 delegate (TheObjectTypeOfC c) { return c.City == London }; 

(the difference is that some lambda expression are delegates, as well as expressions, but please ignore this, you will not need it for some time)

If / when the compiler cannot infer parameter types, you can force them: (MyObject p) => p.SomeProperty != null . Here you tell the compiler that p is a parameter.

While I showed you what is called a “lambdas expression”, you can even make a “lambdas expression”:

 p => { for (int i = 0; i < 10; i++) { if (p.SomeProperty == 0) { return true; } } return false; } 

(I won’t tell you what the “backstage” differences between lambdas and statement lambdas expressions are. If you want to know them, read the msdn page I pointed out earlier)

+1
source

John has already answered, but I would like to add this.

In the example you provided, assume that Linq spans all clients, and c is just a reference to each element in the enumerated:

 var result = new List<Customer>(); foreach(var c in customers) { if (c.City == "London") result.Add(c); } return result; 

This variable, like any other, it should not be one named, but it is only a conditional agreement.

+1
source

You do not need to use lambda expressions on Lİnq for sql or Entities; here is an alternative way to your code;

the code:

 customers.Where(c => c.City == "London"); 

another way;

  var query = from cs in customers where cs.City == "London" select cs; 

This is a different way. it depends on you.

+1
source

No, this is optional.

We have two ways to record LINQ queries.

One is a query method and the other is a construction method. You only need to add the lambda expression in the case of the build method. For example, if we want to find all these students from the Students object that have more grades than 70. but we can do this in LINQ with the following syntax

  var data = from p in stdList where p.marks > 70 select p; 

or var data = stdList.Where (p => p.marks> 70);

a later approach is a construction method, in the Where function we pass lambda expressions.

Lambda expressions are just short abbreviations that you can always use in LINQ queries, but if you want to avoid the syntax of the whole query to just apply a simple condition, you can simply use LINQ building methods (which query lambda expressions) in lambda expressions , you always define some alias and then do your operation.

As for the => operator, it works just like an assignment operator. For example:

 (p) => p.Gender == "F" It means "All persons p, such that person's Gender is F" 

In some literature, this is called a predicate. Another literary terminology is Projection.

 (p) => p.Gender ? "F" : "Female" "Each person p becomes string "Female" if Gender is "F"" 

This is a projection, it uses a ternary operator. Although I have explained very simple examples, but I hope this helps you., :)

+1
source

All Articles