Overview Of Let In LINQ

In general, Let allows creating a local variable at the LINQ query. Let has the same operation as to build local variable in a loop.

This example has the analogy between a loop and LINQ query.

LINQ

Let is allowed only in LINQ queries, so it can’t be used in Lambdas.

Example code

This is the classes we will use in our examples.

Shop class

  1. using System.Collections.Generic;  
  2.    
  3. namespace LetKeyword  
  4. {  
  5.     public class Shop  
  6.     {  
  7.         public int               Id    { get; set; }  
  8.         public string            Name  { get; set; }  
  9.         public IEnumerable<Sale> Sales { get; set; }  
  10.     }  
  11. }  

Note

The Shop class has an IEnumerable<Sale> property with the associated sales.

Sale class

  1. using System;  
  2.    
  3. namespace LetKeyword  
  4. {  
  5.     public class Sale  
  6.     {  
  7.         public int      Id     { get; set; }  
  8.         public DateTime Date   { get; set; }  
  9.         public decimal  Amount { get; set; }  
  10.     }  
  11. }  

The ShoppingDB class is a virtual DB process.

  1. using System;  
  2. using System.Collections.Generic;  
  3.    
  4. namespace LetKeyword  
  5. {  
  6.     public static class ShoppingDB  
  7.     {  
  8.    
  9.         public static IEnumerable<Shop> GetShops()  
  10.         {  
  11.             var result = new List<Shop>()  
  12.             {  
  13.                 new Shop  
  14.                 {  
  15.                     Id    = 1,  
  16.                     Name  = "Shop 1",  
  17.                     Sales = new List<Sale>()  
  18.                     {  
  19.                         new Sale{ Id = 1,  Date = new DateTime(2017,01,02), Amount =  1520m },  
  20.                         new Sale{ Id = 8,  Date = new DateTime(2017,01,26), Amount =   500m },  
  21.                         new Sale{ Id = 25, Date = new DateTime(2017,02,15), Amount =  8900m },  
  22.                         new Sale{ Id = 26, Date = new DateTime(2017,02,28), Amount = 40000m },  
  23.                         new Sale{ Id = 39, Date = new DateTime(2017,03,02), Amount = 75000m }  
  24.                     }  
  25.                 },  
  26.                 new Shop  
  27.                 {  
  28.                     Id    = 2,  
  29.                     Name  = "Shop 2",  
  30.                     Sales = new List<Sale>()  
  31.                     {  
  32.                         new Sale{ Id = 2,  Date = new DateTime(2017,01,06), Amount =     10m },  
  33.                         new Sale{ Id = 3,  Date = new DateTime(2017,01,08), Amount =   3000m },  
  34.                         new Sale{ Id = 11, Date = new DateTime(2017,02,11), Amount = 100000m },  
  35.                         new Sale{ Id = 12, Date = new DateTime(2017,02,12), Amount = 515000m },  
  36.                         new Sale{ Id = 42, Date = new DateTime(2017,03,12), Amount =     25m },  
  37.                         new Sale{ Id = 43, Date = new DateTime(2017,03,12), Amount =    200m },  
  38.                         new Sale{ Id = 52, Date = new DateTime(2017,03,16), Amount =    300m }  
  39.                     }  
  40.                 },  
  41.                 new Shop  
  42.                 {  
  43.                     Id    = 3,  
  44.                     Name  = "Shop 3",  
  45.                     Sales = new List<Sale>()  
  46.                     {  
  47.                         new Sale{ Id = 13,  Date = new DateTime(2017,02,12), Amount = 2500m },  
  48.                         new Sale{ Id = 14,  Date = new DateTime(2017,02,12), Amount = 3000m }  
  49.                     }  
  50.                 },  
  51.                 new Shop  
  52.                 {  
  53.                     Id    = 4,  
  54.                     Name  = "Shop 4",  
  55.                     Sales = new List<Sale>()  
  56.                     {  
  57.                         new Sale{ Id = 15, Date = new DateTime(2017,01,13), Amount =  79000m },  
  58.                         new Sale{ Id = 16, Date = new DateTime(2017,01,13), Amount =   6000m },  
  59.                         new Sale{ Id = 53, Date = new DateTime(2017,03,17), Amount = 145000m },  
  60.                         new Sale{ Id = 54, Date = new DateTime(2017,03,17), Amount =   5000m },  
  61.                         new Sale{ Id = 55, Date = new DateTime(2017,03,18), Amount =  37800m },  
  62.                         new Sale{ Id = 56, Date = new DateTime(2017,03,19), Amount =  11200m },  
  63.                         new Sale{ Id = 57, Date = new DateTime(2017,03,26), Amount =  22580m },  
  64.                         new Sale{ Id = 58, Date = new DateTime(2017,04,01), Amount =   1000m },  
  65.                         new Sale{ Id = 59, Date = new DateTime(2017,04,02), Amount =   9000m },  
  66.                         new Sale{ Id = 60, Date = new DateTime(2017,04,03), Amount = 990000m },  
  67.                         new Sale{ Id = 61, Date = new DateTime(2017,04,04), Amount =   8000m },  
  68.                         new Sale{ Id = 62, Date = new DateTime(2017,04,05), Amount =  52580m },  
  69.                         new Sale{ Id = 63, Date = new DateTime(2017,04,06), Amount = 558900m },  
  70.                         new Sale{ Id = 64, Date = new DateTime(2017,04,07), Amount =  88900m }  
  71.                     }  
  72.                 }  
  73.             };  
  74.    
  75.    
  76.             return result;  
  77.    
  78.         }  
  79.    
  80.    
  81.    
  82.    
  83.     }  
  84. }  

Benefits of let

The main benefits of Let are:

  • Reading compression code.
  • Encapsulate functionality.
  • Improvement performance.

Reading compression code

The let keyword provides us the improvement in reading and compression code because unit calls and calculations transform it in more readable.

In this example, we build a query where the shop has sales in March and the number of sales in each shop is a pair.

Example without let,

  1. [TestMethod]  
  2. public void ReadingCompressionCode_WithoutLet()  
  3. {  
  4.     var result = from shop in ShoppingDB.GetShops()  
  5.    
  6.                  where shop.Sales.Any(s => s.Date.Month == 3)   
  7.                     && shop.Sales.Count() % 2 == 0  
  8.    
  9.                  select shop;  
  10. }  

 Example with let,

  1. [TestMethod]  
  2. public void ReadingCompressionCode_WithLet()  
  3. {  
  4.     var result = from shop in ShoppingDB.GetShops()  
  5.    
  6.                  let hasMarchSales = shop.Sales.Any(s => s.Date.Month == 3)  
  7.                  let hasPairSales = shop.Sales.Count() % 2 == 0  
  8.    
  9.                  where hasMarchSales && hasPairSales  
  10.    
  11.                  select shop;  
  12. }  

Comparison

LINQ

Encapsulate functionality

Another Let advantage is encapsulating functionality. If we do good work with Let in our LINQ queries, we only need to change the code at one place in our code for refactoring.

Example without let,

  1. [TestMethod]  
  2. public void EncapsulateFunctionality_WithoutLet()  
  3. {  
  4.     var result = from shop in ShoppingDB.GetShops()  
  5.    
  6.                     where shop.Sales.Average(a => a.Amount) > 1000  
  7.                        && shop.Sales.Average(a => a.Amount) < 100000  
  8.    
  9.                     select shop;  
  10. }  

Example with let,

  1. [TestMethod]  
  2. public void EncapsulateFunctionality_WithLet()  
  3. {  
  4.     var result = from shop in ShoppingDB.GetShops()  
  5.    
  6.                     let myAverage = shop.Sales.Average(a => a.Amount)  
  7.    
  8.                     where myAverage > 1000   
  9.                        && myAverage < 100000  
  10.    
  11.                     select shop;  
  12. }  

Comparison

LINQ

With the let solution, in change case, only we will modify in one place, without let, we will modify in 2 or N places.

Improvement performance

In the previous without let example, we have seen how we wrote the same code two times for the same goal. As with we wrote the code two times, the code will be executed two times too with a lost performance.

This problem is greater if our query has a select extender method, because add another execution.

Example without let,

  1. [TestMethod]  
  2. public void ImprovementPerformance_WithoutLet()  
  3. {  
  4.     var result = from shop in ShoppingDB.GetShops()  
  5.    
  6.                     where shop.Sales.Average(a => a.Amount) > 1000  
  7.                        && shop.Sales.Average(a => a.Amount) < 100000  
  8.    
  9.                     select new  
  10.                     {  
  11.                         Id           = shop.Id,  
  12.                         Name         = shop.Name,  
  13.                         Sales        = shop.Sales,  
  14.                         SalesAverage = shop.Sales.Average(a => a.Amount)  
  15.                     };  
  16. }  

Example with let,

  1. [TestMethod]  
  2. public void ImprovementPerformance_WithLet()  
  3. {  
  4.     var result = from shop in ShoppingDB.GetShops()  
  5.    
  6.                     let myAverage = shop.Sales.Average(a => a.Amount)  
  7.    
  8.                     where myAverage > 1000  
  9.                        && myAverage < 100000  
  10.    
  11.                     select new  
  12.                     {  
  13.                         Id           = shop.Id,  
  14.                         Name         = shop.Name,  
  15.                         Sales        = shop.Sales,  
  16.                         SalesAverage = myAverage  
  17.                     };  
  18. }  

Comparison

LINQ

The example with Let hast better performance than the without Let example, because the first one executes one time and the second one execues two time for each item.

We have added two tests to show method performance:

 
  1. [TestMethod]  
  2. public void ImprovementPerformance_WithoutLet3()  
  3. {  
  4.     var stopWatch = new Stopwatch();  
  5.     stopWatch.Start();  
  6.   
  7.     var data = Enumerable.Range(0, 30000 - 1).ToList();  
  8.   
  9.   
  10.     var result = (from s in data  
  11.   
  12.                  where data.Average(a => a) > s  
  13.                     && data.Average(a => a) < 100  
  14.   
  15.                  select new  
  16.                  {  
  17.                      Number = s,  
  18.                      Average = data.Average(a => a)  
  19.                  }).ToList();  
  20.   
  21.     stopWatch.Stop();  
  22.   
  23.     System.Diagnostics.Trace.WriteLine($"{stopWatch.Elapsed.TotalMilliseconds}");  
  24. }  

Output --> 17803.3522 miliseconds

 
  1. [TestMethod]  
  2. public void ImprovementPerformance_WithLet4()  
  3. {  
  4.   
  5.     var stopWatch = new Stopwatch();  
  6.     stopWatch.Start();  
  7.   
  8.     var data = Enumerable.Range(0, 30000 - 1).ToList();  
  9.   
  10.   
  11.     var result = (from s in data  
  12.   
  13.                     let average = data.Average(a => a)  
  14.   
  15.                     where average > s  
  16.                         && average < 100  
  17.   
  18.                     select new  
  19.                     {  
  20.                         Number = s,  
  21.                         Average = average  
  22.                     }).ToList();  
  23.   
  24.     stopWatch.Stop();  
  25.   
  26.     System.Diagnostics.Trace.WriteLine($"{stopWatch.Elapsed.TotalMilliseconds}");  
  27. }   

Output --> 12497.8096 miliseconds

With Let is faster.

 
 
 
 
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all