7 Answers
7

It's just a very concise way of writing a simple function that returns a value. It's called an "anonymous function" because it's never assigned a name or a formal definition (the parameter types and the return type are inferred from the context).

(Actually, it's not just shorthand; lambda expressions are related to some other constructs called closures, which are very cool and powerful tools.)

That's assuming LINQ to Objects, of course. For LINQ to SQL etc, you'd need to build an expression tree.

As to why "=>" is always used in a lambda expression, that's simply because that's the way the operator is written - it's like asking why "+" is used for addition.

A lambda expression of "c => ..." is effectively giving the lambda expression a parameter called c... in this case generic type inference provides the type of c. The body provides either an action to perform or some calculation to return a value based on c.

A full-blown description of lambda expressions is beyond the scope of this answer. As a blatant plug for my book, however, they're covered in detail in chapter 9 of C# in Depth.

Lambda and linq are quite separate. You can use one without using the other (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 (it's much more complex) you can use a lambda expression to make a local function. what you put before the => (in your example the c) will be the parameter of the function. The return type is "discovered" by the C# compiler.

c => c.City == "London" is nearly equivalent to:

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

(the difference is that some lambda expression are delegates and also expressions, but please ignore this, you won't need it for some time)

If/when the compiler isn't able to infer the types of the parameters, you can force them: (MyObject p) => p.SomeProperty != null. Here you are telling the compiler that p is a parameter.

While here I showed you what are called "expression lambdas", you can even do "statement lambdas":

They're not totally separate, in that the translation of query expressions (which are part of LINQ) relies on lambda expressions. However it is fair to say that you can use the LINQ libraries without using lambda expressions.
–
Jon SkeetFeb 22 '11 at 18:45

One is query method and other is builder method. You only need to put lambda expression in case of builder method.
For example, if we want to find all those students from some Students object that have more marks than 70.
but we can do this thing in LINQ with following syntax

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

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

later approach is builder method, in Where function, we are passing lambda expressions.

Lambda expressions are just short cuts of doing things you can always use LINQ queries but if you want to avoid whole query syntax for just applying a simple condition you can just use LINQ builder methods (which asks for lambda expressions) in lambda expressions, you always define some alias and then perform your operation.

As far as => operator is concerned, It works just like assignment operator.
For example: