If Linq uses a "for or while" loop internally for the where clause, Why should we not use it directly?
Linq WHERE condition internal work
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
If Linq uses a "for or while" loop internally for the where clause, Why should we not use it directly?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 15, 2025, 1:11 PM EST
While LINQ is a powerful tool, there are cases where manual loops might be more suitable:
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 15, 2025, 1:44 PM EST
var evenNumbers = numbers.Where(n => n % 2 == 0);var evenNumbers = new List(); foreach (var n in numbers) { if (n % 2 == 0) { evenNumbers.Add(n); } }When using
fororwhileloops directly, you need to manually handle iteration, conditional checks, and modifications to the collection, which can lead to common errors like off-by-one errors, skipping items, or modifying collections during enumeration. LINQ communicates what you want to do, while loops communicate how to do it. This high-level abstraction makes LINQ easier to understand at a glance.