Do you use the filtering parameter in LINQ methods?

Updated by Bryden Oliver 3 years ago. See history

123
<introEmbed body={<> Many LINQ methods like Count, First and so on include an optional filter parameter. It's normally much more readable to use this than add an extra call to Where </>} />
.Where(x => x < 5).Count()
.Where(x => x < 5).FirstOrDefault()

❌ Figure: Figure: Bad example - More code that requires extra thought to understand.

.Count(x => x < 5)
.FirstOrDefault(x => x < 5)

✅ Figure: Figure: Good example - Shorter and easier to read.

Acknowledgements

Bryden Oliver
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.