Programming Tips 💡
51.6K subscribers
67 photos
10 videos
30 files
354 links
Programming & AI:
Tips 💡
Articles 📕
Resources 👾
Design Patterns 💎
Software Principles

🇳🇱 Contact: @MoienTajik

🎯 Buy ads: https://telega.io/c/ProgrammingTip
Download Telegram
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 :

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