score:5
FirstOrDefault
provides two logical paths to handle whether there is a data result or there is not. Hence its handling is only local to the scope which it is called in. First
provides a method to jump out of the local scope via an exception.
Design WHY
First
only returns data for the current scope but gives the ability to handle any unusual or unforseen circumstance of no data and will handle that as an application type error via an Exception
. That exception can be handled by a centralized controller outside of the current scope.
Hence it depends on how one wants to handle the lack of data and whether to handle it locally or outside of the local scope and to what degree the no data situation should be processed.
why Microsoft thought there should be two different methods for this?
Flexibility for the programmer to code for different situations as needs dictate.
score:9
First
throws an exception if the sequence is empty. This is the only difference.
First
has a built-in assertion that the sequence is not empty. That is it's purpose. Sometimes you know that the sequence cannot be empty. For example if you look up a customer in database A and it exists you know it will exist in database B as well. If it ever does not exist in B this would be a bug.
In case of a bug you want a loud bang to alert you of that. That's why assertions are often useful in production code. You certainly do not want to carry on executing with bad data. This makes finding the origin of bug harder, or might even hide the bug completely.
The whole point of First
is that it is often super useful to have this built-in assertion.
The same is true for Single
and SingleOrDefault
. These assert that the sequence can never have more than one element. It turns out that this is often what you want in real-world code.
FirstOrDefault
also allows you to handle the case where the sequence is empty whereas First
does not (catching the exception would be a misuse of exceptions).
Also, First
documents your knowledge about the sequence length so that this fact is obvious to anyone who reads the code.
Source: stackoverflow.com
Related Articles
- Why doesn't LINQ's First method simply return null if no item is found?
- Return first item in list if FirstOrDefault returns null
- How do I return the first item that matches a predicate or the simply first item if none match?
- EF Code First comparing null values generates strange query
- LINQ query to return whether an item is found in an array?
- 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' could be found
- trying to get first item in collection returns NULL
- Linq expression to return the first overloaded method that takes 1 string parameter
- most efficient Entity Framework Code First method of flattening / projecting parent entity with specific child
- Need to join two strings if both contain values, or return one value if first is NULL
- int' does not contain a definition for 'contains' and no extension method 'contains' accepting a first argument of type 'int' could be found
- Linq query where First() appears to return null when results are not found
- Entity Framework Code First ToList method timing out on SQL Azure
- Return First Item from Multiple Sources with Where() foreach return
- LINQ obtaining first item not empty or null from Dictionary
- How do I use LINQ to return the first item from each set of Grouped data (In VB.Net)
- Using Enumerable.Contains method for null item checking
- Entity Framework Code First Select Item Based on Relationship
- How to return null value when using CopyToDataTable() LINQ extension method
- LINQ query to return first item in an ordered grouping
- Select Linq statement needs to return 0 if either item is null
- How to select first list item to null using Linq
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- Find() and First() throws exceptions, how to return null instead?
- Linq OrderByDescending, null first
- Entity Framework, Code First and Full Text Search
- Why is there no Linq method to return distinct values by a predicate?
- How to return anonymous type from c# method that uses LINQ to SQL
- Return null for FirstOrDefault() on empty IEnumerable<int>?
- SQL to LINQ Expression
- Reading only specific rows from Excel using ClosedXML
- Grouping and flattening list with linq and lambda
- How can I check for a NullReferenceException in this C# LINQ to XML statement?
- Filter data from DataTable
- LINQ query with multiple LEFT JOIN
- How merge two lists of different objects?
- How to pass a user-input string as a field name to access a field within an object?
- linq equals override
- Entity Framework linq join/association
- LINQ to map a datatable into a list<MyObject>
- Grouping in Linq ( on multiple fields)
- How to get a key value pair out of an array of objects
- Displaying non-existing(in database table) column in DataGridView
- LINQ Group By and merge properties
- Get XML attribute Values by its Descendants
- Totalling inner lists by different methods on a list using Linq
- LINQ queries with many-to-many tables in Entity Data Model
- how to use left join in entity framework query?
- NotSupportedException when using lambda against a DataContext and a generic function constraint of interface