locked
What is let keyword RRS feed

  • Question

  • What is the use of let keyword. How to use the keyword
    Monday, July 15, 2013 11:53 AM

Answers

  • It might be helpful to see an example where the let clause introduces a variable that is not just a trivial alias for another symbol, and is reused elsewhere in the query in a non-trivial way.

    var query = from product in products
                let discount = product.price * 0.15m
                where discount < 50
                select product.price - discount;

    It's also worth noting that with LINQ it's trickier to introduce another variable with the lambda expression.

    // Not so good:
    var query = products
       .Where( product => product.price * 0.15m < 50 )
       .Select( product => product.price - (product.price * 0.15m) );
    // Darn! Had to write out the calculation of the discount twice.
    
    // Better:
    var query = products
       .Select( product => new { prod = product, discount = product.price * 0.15m } )
       .Where( anon => anon.discount < 50 )
       .Select( anon => anon.prod.price - anon.discount );
    // Intermediate select of an anonymous object is functionally similar to the "let" clause.



    • Edited by Wyck Monday, July 15, 2013 12:46 PM minor cleanup
    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:51 PM
    • Unmarked as answer by R A J Kumar S Monday, July 15, 2013 4:52 PM
    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:52 PM
    Monday, July 15, 2013 12:45 PM
  • "Let" - is a part of a query expression.

    It introduces a variable and it can be reused elsewhere in the query.

    This can be make use in complex query expression to make expression simpler.

    ex: 

    int[] array = { 1, 3, 5, 7, 9 }; var result = from a in array let v = a where v >= 5 select v;

    "Let" key word can be used only when a new variable must be computed and reused.

    Here, v - is a variable and is used twice.


    Kavithaa.N


    • Edited by Kavitha_123 Monday, July 15, 2013 12:20 PM Edit
    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:51 PM
    Monday, July 15, 2013 12:11 PM
  • The Let keyword allows storing the results of a query which can be used in a subsequent query

    var em = from e in emp
                         group e by new { e.Salary, e.Id }
                             into gEmp
                             let avgsal = (gEmp.Sum(t => t.Salary) / gEmp.Count())
                             where gEmp.Key.Salary == avgsal
                             select new { gEmp.Key.Salary, gEmp.Key.Id };

    http://msdn.microsoft.com/en-us/library/bb383976.aspx

    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:51 PM
    Monday, July 15, 2013 4:34 PM

All replies

  • "Let" - is a part of a query expression.

    It introduces a variable and it can be reused elsewhere in the query.

    This can be make use in complex query expression to make expression simpler.

    ex: 

    int[] array = { 1, 3, 5, 7, 9 }; var result = from a in array let v = a where v >= 5 select v;

    "Let" key word can be used only when a new variable must be computed and reused.

    Here, v - is a variable and is used twice.


    Kavithaa.N


    • Edited by Kavitha_123 Monday, July 15, 2013 12:20 PM Edit
    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:51 PM
    Monday, July 15, 2013 12:11 PM
  • It might be helpful to see an example where the let clause introduces a variable that is not just a trivial alias for another symbol, and is reused elsewhere in the query in a non-trivial way.

    var query = from product in products
                let discount = product.price * 0.15m
                where discount < 50
                select product.price - discount;

    It's also worth noting that with LINQ it's trickier to introduce another variable with the lambda expression.

    // Not so good:
    var query = products
       .Where( product => product.price * 0.15m < 50 )
       .Select( product => product.price - (product.price * 0.15m) );
    // Darn! Had to write out the calculation of the discount twice.
    
    // Better:
    var query = products
       .Select( product => new { prod = product, discount = product.price * 0.15m } )
       .Where( anon => anon.discount < 50 )
       .Select( anon => anon.prod.price - anon.discount );
    // Intermediate select of an anonymous object is functionally similar to the "let" clause.



    • Edited by Wyck Monday, July 15, 2013 12:46 PM minor cleanup
    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:51 PM
    • Unmarked as answer by R A J Kumar S Monday, July 15, 2013 4:52 PM
    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:52 PM
    Monday, July 15, 2013 12:45 PM
  • Just to make certain there is no misscommunication:

    Let is a Keyword that only works in combination with Linq Queries? I found it on the List of Keywords, under contextual Keywords:

    http://msdn.microsoft.com/en-us/library/bb383976.aspx


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Monday, July 15, 2013 4:10 PM
  • The Let keyword allows storing the results of a query which can be used in a subsequent query

    var em = from e in emp
                         group e by new { e.Salary, e.Id }
                             into gEmp
                             let avgsal = (gEmp.Sum(t => t.Salary) / gEmp.Count())
                             where gEmp.Key.Salary == avgsal
                             select new { gEmp.Key.Salary, gEmp.Key.Id };

    http://msdn.microsoft.com/en-us/library/bb383976.aspx

    • Marked as answer by R A J Kumar S Monday, July 15, 2013 4:51 PM
    Monday, July 15, 2013 4:34 PM
  • Just to make certain there is no misscommunication:

    Let is a Keyword that only works in combination with Linq Queries? I found it on the List of Keywords, under contextual Keywords:

    http://msdn.microsoft.com/en-us/library/bb383976.aspx


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    The question mark in your quoted text above is the only thing uncertain.  If you replace it with a period, you are absolutely correct.

    So, at the risk of saying this needlessly, but in the spirit of giving examples:

    let is a context sensitive keyword, in that it has meaning, but only in a certain context.  In this case, the context for let is that of a query clause.  let can be used as an identifier in many other contexts.  For example:

    class Program
    {
        class Foo
        {
            public static int let; // a field
        }
        class Bar
        {
            public static int let( int x ) // a method
            {
                return x + 1;
            }
        }
        class Baz
        {
            public static Func<int, int> fn = (let => let + 1); // a lambda variable
        }
    
        class let // a class name
        {
            public int x; 
        };
    
        static void Main( string[] args )
        {
            int let = 5; // a local variable
            let temp = new let() { x = let };
            Foo.let = Bar.let( Baz.fn(temp.x) );
        }
    }
    

    ...just to give a few examples there are many other contexts in which let could be used as an identifier.

    let cannot be used as an identifier in a query clause.

    Here are some examples where you cannot use let as an identifier.

    int[] numbers = new int[] {1, 3, 5, 7, 9};
    var query = from let in numbers // Identifier expected
                select let; // Invalid expression term
    var query2 = from x in numbers
                 let let = x + 1 // second let gives: Identifier expected
                 select x;
    
    


    • Proposed as answer by Jason Dot Wang Wednesday, July 24, 2013 7:46 AM
    Monday, July 15, 2013 6:50 PM