15

It's amazing how little information there is on this. I found tons of tutorials explaining LINQ, but they don't explain this particular operator:

var Results = UserFavoritesContext.UserFavorites.Select(color => color.FavoriteColor);

"x => x.y"

Can someone please explain how this works? I get the general syntax and am able to use it to make queries, but it's like doing something without knowing what you're doing.

user3407764
  • 463
  • 1
  • 5
  • 15

7 Answers7

28

Suppose you have a list of people, and you want to iterate over them. You would write something like:

foreach(var person in people)
{
       //do something to person
}

Note how you yourself chose the name person. It could've been any word, but you basically said "process every single item of the list as my person variable".

Now look at this LINQ query:

filteredPeopleList = people.Where(person => person.Name == "John");

Again, you basically chose person as a placeholder name for every object in the original list (one at a time). The above Linq query is equivalent to

foreach(var person in people)
{
       if(person.Name == "John")
       {
           filteredPeopleList.Add(person);
       }
}

To me, x => x.y is basically saying "for every variable we process (let's call it x), please perform the following operation on it (x.y, get the y property)"

I hope that explains it.

Edit
As a commenter who now deleted his comment mentioned, this isn't exclusively used in LINQ. A lambda expression doesn't have to iterate over an IEnumerable, it can be used to process a single item.
However, LINQ is by far the most common place to encounter lambdas, and I find their use very similar to the foreach loop, which is why I picked this example.

Flater
  • 12,908
  • 4
  • 39
  • 62
3

The => operator is used in lambda expressions.

The best way to think of this is a type of syntax for writing a function, where the left side of the operator is the parameters for the function and the right side is the body of the function e.g. this is a valid use of a lambda expression where its being used like a function:

Func<int, int> incrementFunction = i => i + 1;
int incrementedNumber = incrementFunction(1);

The name i in this case is an arbitrary variable name e.g. just like you would name a functions input parameter. Notice I didnt need to declare the input parameters type because the compiler will infer it. Also notice I dont need to include the "return" keyword or enclose the function body in a code block. It doesn't necessarily need any parameters either e.g.

Action myAction = () => Console.Write("something");
myAction();

When you use it in a linq expression, think of it as though the lambda function is being called on every element in the collection (which I believe is exactly what happens with linq to objects).

rdans
  • 2,179
  • 22
  • 32
2

It's the syntax of a Lambda expression. If it helps you to remember... in a nutshell, the argument to pass (the parameter) is on the left of the => and the method(s) that use it are on the right hand side of it.

I hope this short summary explains it enough :)

VictorySaber
  • 3,084
  • 1
  • 27
  • 45
2

That is a lambda expression, and it can be used as a selector from an object

You can conditionally select (or another operation orderby, count, etc) when the expression is true. For example:

Say you had a list of people and their details: ID, Name, City and Profession.

You could select a single person by using lambda to search for their specific ID:

public Person GetByID(int id)
{
Person selectedPerson = people.SingleOrDefault(person => person.ID == id);
return selectedPerson;
}

Same could be applied for a select on a city, this would be:

public List<Person> GetByCity(string city)
{
List<Person> selectedPeople = people.where(person => person.City == city);
return selectedPeople;
}

The lamda expression is where you place your operation variable, so in these cases the condition upon which you select the data. You can use it as a orderby variable much in the same way, in the next example I use two lamdas to perform two seperate functions

public List<Person> GetByCity(string city)
{
List<Person> selectedPeople = people.where(person => person.city == city)
.OrderByDescending(person => person.Name);
return selectedpeople;
}

I hope this helps at all

nickson104
  • 580
  • 1
  • 7
  • 18
2

x => x.y is Lambda Expression introduced with C# 3.0. The general syntax is

parameter => executioncode

The Lambda Expression has 3 parts:

  1. x on left hand side is the parameter.This can be a variable,delegate or an anonymous function.
  2. => read as "goes to", which acts as separator
  3. x.y is an Expression to be evaluated.

For example, the lambda expression x => x * x specifies a parameter that’s named x and returns the value of x squared (source: MSDN).

Hope this would help you.

Nate
  • 2,449
  • 3
  • 21
  • 29
Adersh M
  • 596
  • 3
  • 19
1

They're called Lamda expression:

https://msdn.microsoft.com/en-us/library/bb397687.aspx

var Results = UserFavoritesContext.UserFavorites.Select(color => color.FavoriteColor);

is similar to:

List<UserFavorite> Results = new List<UserFavorite>();
foreach(var item in UserFavorites)
{
   Results.Add(item.FavoriteColor);
}
User2012384
  • 4,769
  • 16
  • 70
  • 106
1

This is not specific to Linq.

It is the way you write a lambda expression. The arrow => is the delimiter between your lambda parameters and its body.