locked
what is => operator called and what does it do ? RRS feed

  • Question

  • in a lot of telerik controls for ASP.NET MVC, In their samples I found this operator being used, Such as 

     

    TelerikGridObj.Load(Employees => { Employees. Name, Employees.Salary });

     

    In LINQ query, we write something.where(e => e.EmpId = 3); //apparently e here picks up something of some focus.

     

    I need to know where does that come from, what does it do ?

     

     

    Finally my suggestion, Please stop playing with language constructs.


    Fahad
    Monday, March 7, 2011 4:38 PM

Answers

  • (My first reply went AWOL - posting again, but this may end up as a double-post)

    It's a lambda expression. Think of it as a "local function".

    See http://msdn.microsoft.com/en-us/library/bb397687.aspx for details.

    I'll just explain a bit about how that "Where" works.

    Suppose that you have a sequence of things, and you want to filter that sequence so that only its elements that have a certain state are processed. You do this by defining a function that returns a bool and which takes as a parameter an object of the same type as the sequence's elements (this is known as a "predicate", i.e. some function that returns a bool). This predicate will be called for each element in the sequence, and if it is true then that element will be included in the output sequence.

    Imagine that you have a sequence of objects each of which has a ".Count" property, and you want to access just the elements which have a count greater than zero. You can do that as follows:

    var filteredSequence = sequence.Where(x => x.Count > 0);

    What happens is that each element of the sequence is passed as "x" in the little lambda function "x => x.Count > 0", and the return value is simply "x.Count > 0". Whenever that is true, that element is returned in the resultant sequence.

    Incidentally, => is pronounced "Goes to".

    • Marked as answer by Fahad349 Monday, March 7, 2011 9:31 PM
    Monday, March 7, 2011 5:11 PM

All replies

  • (My first reply went AWOL - posting again, but this may end up as a double-post)

    It's a lambda expression. Think of it as a "local function".

    See http://msdn.microsoft.com/en-us/library/bb397687.aspx for details.

    I'll just explain a bit about how that "Where" works.

    Suppose that you have a sequence of things, and you want to filter that sequence so that only its elements that have a certain state are processed. You do this by defining a function that returns a bool and which takes as a parameter an object of the same type as the sequence's elements (this is known as a "predicate", i.e. some function that returns a bool). This predicate will be called for each element in the sequence, and if it is true then that element will be included in the output sequence.

    Imagine that you have a sequence of objects each of which has a ".Count" property, and you want to access just the elements which have a count greater than zero. You can do that as follows:

    var filteredSequence = sequence.Where(x => x.Count > 0);

    What happens is that each element of the sequence is passed as "x" in the little lambda function "x => x.Count > 0", and the return value is simply "x.Count > 0". Whenever that is true, that element is returned in the resultant sequence.

    Incidentally, => is pronounced "Goes to".

    • Marked as answer by Fahad349 Monday, March 7, 2011 9:31 PM
    Monday, March 7, 2011 5:11 PM
  • Please do not post double posts. One will be more then enough. Ok?

    Original one is here.

    Monday, March 7, 2011 5:12 PM