Programming & AI Tips 💡
45.9K subscribers
67 photos
11 videos
30 files
387 links
Programming & AI:
Tips 💡
Articles 📕
AI & LLMs 👾
Design, Architecture, Principles ✅

🇳🇱 Contact: @MoienTajik
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