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:35 PM

Answers

  • Ok I will give you an example. In following example, have array of 5 words. Now what I want is to get the length of the shortest string present in array of string.

    One method is to use for loop to compare length every string, but what if I can find out length of each string, then just get minimum or maximum out of that using a built in function, like min() or max() similarely which we have in SQL Transect.It will be easy for me and I don't have to write much code.

    In following example, I have array of 5 strings. and then I have a statement to calculate min length a string has in that array of strings. Now what that statement is doing?

    As I have used a built in function Min() of string array(a), So the temp variable will only know about strings present in array a. So it will iterate through each element of array a, and record their length. And then Min() function finds out min length out of those all lengths that inner statement returns.

    What that inner statement means -- I want to use temp variable to iterate through the array and want to record their length.

    string[] a = { "hello", "go", "here", "there", "forum" };
    int max = a.Max(temp => temp.Length); 
    int min = a.Min(temp1 => temp1.Length);

     So we get what we want using a single statement.

    You might be thinking I haven't declared temp anywhere, but actually C# is declaring this temp variable of anonymous type. You can also declare this to be of string type there, but this code will work fine.

    Note that inner statement is declaring temp as new variable, if you have another variable temp declared globally or in the same scope where you have typed this statement, it will show you an error, that there is another variable temp already exists. So, you don't have to declare it outside from this statement.

    Now if a want to get the string having length equal to the maximum length which we get in upper statement, use following statement.

     

    IEnumerable<string> maximum = a.Where(temp2 => temp2.Length == max);
    foreach (string s in maximum)
    {
      Console.WriteLine(s);
    }

    Here type of maximum is IEnumerable, go to the following link to read about IEnumerable

    http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx


    How To Articles
    • Edited by rt.garg1991 Monday, March 7, 2011 5:47 PM
    • Marked as answer by Fahad349 Monday, March 7, 2011 9:29 PM
    Monday, March 7, 2011 5:28 PM
  • This is a Lambda Expression operator. All lambda expressions use the lambda operator ">=, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x."

    You can read more here.

    Mitja

    • Marked as answer by Fahad349 Monday, March 7, 2011 9:29 PM
    Monday, March 7, 2011 5:04 PM

All replies

  • This is a Lambda Expression operator. All lambda expressions use the lambda operator ">=, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x."

    You can read more here.

    Mitja

    • Marked as answer by Fahad349 Monday, March 7, 2011 9:29 PM
    Monday, March 7, 2011 5:04 PM
  • Ok I will give you an example. In following example, have array of 5 words. Now what I want is to get the length of the shortest string present in array of string.

    One method is to use for loop to compare length every string, but what if I can find out length of each string, then just get minimum or maximum out of that using a built in function, like min() or max() similarely which we have in SQL Transect.It will be easy for me and I don't have to write much code.

    In following example, I have array of 5 strings. and then I have a statement to calculate min length a string has in that array of strings. Now what that statement is doing?

    As I have used a built in function Min() of string array(a), So the temp variable will only know about strings present in array a. So it will iterate through each element of array a, and record their length. And then Min() function finds out min length out of those all lengths that inner statement returns.

    What that inner statement means -- I want to use temp variable to iterate through the array and want to record their length.

    string[] a = { "hello", "go", "here", "there", "forum" };
    int max = a.Max(temp => temp.Length); 
    int min = a.Min(temp1 => temp1.Length);

     So we get what we want using a single statement.

    You might be thinking I haven't declared temp anywhere, but actually C# is declaring this temp variable of anonymous type. You can also declare this to be of string type there, but this code will work fine.

    Note that inner statement is declaring temp as new variable, if you have another variable temp declared globally or in the same scope where you have typed this statement, it will show you an error, that there is another variable temp already exists. So, you don't have to declare it outside from this statement.

    Now if a want to get the string having length equal to the maximum length which we get in upper statement, use following statement.

     

    IEnumerable<string> maximum = a.Where(temp2 => temp2.Length == max);
    foreach (string s in maximum)
    {
      Console.WriteLine(s);
    }

    Here type of maximum is IEnumerable, go to the following link to read about IEnumerable

    http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx


    How To Articles
    • Edited by rt.garg1991 Monday, March 7, 2011 5:47 PM
    • Marked as answer by Fahad349 Monday, March 7, 2011 9:29 PM
    Monday, March 7, 2011 5:28 PM
  • Well said mate :) Vote for your and for a huge text written.

    Mitja

    Monday, March 7, 2011 5:29 PM
  • Ok I understand the basics now. How is it determined what would the left hand side of the operator be assigned to?

     

     

     


    Fahad
    Monday, March 7, 2011 9:30 PM
  • Fahad, As I have already said that this variable is used to iterate through the whole array and takes their value one by one. So this statement will execute 5 times in my case, and for every time its value changes from 1st element of array to last element of array.

    But this inner statement is considered as a child scope, that means temp is declared as variable in that statement will only be available till that statement is running. Once that statement got executed fully, scope of temp variable ends and you can't access that variable.

    This is just similar as you declare a variable in if or for statement and try to access it outside from that statement.


    How To Articles
    Monday, March 7, 2011 9:54 PM