When to use First and when to use FirstOrDefault with LINQ

Опубликовано: 03 Октябрь 2024
на канале: Coders Media
4,156
like

I would use First() when I know or expect the sequence to have at least one element. In other words, when it is an exceptional occurrence when the sequence is empty.

Use FirstOrDefault() when you know that you will need to check whether there was an element or not. In other words, when it is legal for the sequence to be empty. You should not rely on exception handling for the check. (It is bad practice and might hurt performance).

umbraco playlist :    • Creating Basic Site  umbraco tutorial  

Angularjs playlist :    • [Part1]AngularJS-Your First AngularJS...  

jquery playlist :    • Refresh DIV Content Without Reloading...  

http://gofreelancertutorial.blogspot.in/