Scott Selby
Scott Selby

Reputation: 9570

simple LINQ query =>

This is an example out of a book using northwind database. What is the => mean ?

   Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;InitialCatalog=Northwind");
   var orders = db.Customers
   .Where(c => c.Country == "USA" && c.Region == "WA")
   .SelectMany(c => c.Orders);
    Console.WriteLine(orders.GetType());

why don't they just write

   Where(c.Country == "USA" && c.Regian == "WA")

Upvotes: 0

Views: 206

Answers (4)

sblom
sblom

Reputation: 27343

The part inside the parentheses of the Where() needs to be a function that takes a Customer and returns a boolean value (that's Func<Customer, bool> in C#'s type system).

It's most common to write that function using C#'s Lambda Expressions.

The c => part means "take the Customer that's passed in to you and call it c. Then with that value of c, compute this conditional. Without the c on the left side of the =>, the compiler would have no way of knowing what c refers to.

Upvotes: 3

phoog
phoog

Reputation: 43046

It's a lambda expression. It describes a function; the variables to the left of the arrow are parameters to the function. In this case, the function has one parameter, so there is a single variable to the left of the arrow.

Consider the "why don't they..." example you gave. Where is c declared? How does the compiler know what it represents?

You may wonder how the compiler knows the type of the c parameter, which it obviously does, because it allows you to call the Country and Region properties. This is accomplished with type inference. The Where method requires a delegate argument with a parameter whose type depends on the type of the sequence (Customers, in this case).

Since Customers is an IEnumerable<Customer>, the compiler expects a delegate with a single Customer parameter, and it therefore treats the single parameter of the lambda expression as a Customer variable.

Upvotes: 1

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

=> is the lambda operator See here: msdn

It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible; they are used extensively in LINQ queries that are expressed in method syntax.

Upvotes: 0

yamen
yamen

Reputation: 15618

If they wrote this:

Where(c.Country == "USA" && c.Regian == "WA")

Where would you define what the variable c referred to? What if somewhere earlier in my code I had written:

var c = "this is a string";

This notation defines a lambda construct, where c => is bound to the delegate input expected by the Where function - in this case, c is bound to each row, and the function you run with c needs to return a boolean.

Note that you could have written a function like this:

public bool OnlyWA(Row c)
{
    return c.Country == "USA" && c.Regian == "WA";
}

And then used it like this:

var orders = db.Customers
               .Where(OnlyWA)
               .SelectMany(c => c.Orders);

Here you don't need the c any longer because you're using a named function not a lambda variable. The only parameter to the function acts in place of the c. This is usually overkill for small functions, and further makes it harder to read as the small predicate function is moved away from its usage (and therefore contextual) location.

Upvotes: 1

Related Questions