Let in LINQ 🔮
A let clause introduces a new range variable with a value that can be based on other range variables.
❌ Sorting by the lengths of usernames without let clause :
✅ Using a let clause to remove redundant calculations :
〰〰〰〰〰〰
#LINQ #Query
@ProgrammingTip
A let clause introduces a new range variable with a value that can be based on other range variables.
❌ Sorting by the lengths of usernames without let clause :
var query =
from user in SampleData.AllUsers
orderby user.Name.Length
select user.Name;
foreach (var name in query)
Console.WriteLine("{0}: {1}", name.Length, name);
✅ Using a let clause to remove redundant calculations :
var query =
from user in SampleData.AllUsers
let length = user.Name.Length
order by length
select new
{
Name= user.Name,
Length = length
};
foreach (var entry in query)
Console.WriteLine("{0}:{1}",entry.Length, entry.Name);
〰〰〰〰〰〰
#LINQ #Query
@ProgrammingTip