First() - There is at least one result, an exception is thrown if no result is returned.FirstOrDefault() - Same as First(), but not thrown any exception or return null when there is no result.
First():- If you will use First() in your query then it will return at least one record for you according to your condition but if it will not find any data than it throws an exception.FirstOrDefault():- It also works like First() as it will also return at least one record but it will not throw an exception if it will not find any record as per your condition. Instead of throwing an exception it will return null as a default value.
First gives error if data is not present for given condition. FirstOrDeafult gives no error even if data is not present for given condition ,it returns null.
The First() method returns the first element of a collection, or the first element that satisfies the specified condition using lambda expression or Func delegate. If a given collection is empty or does not include any element that satisfied the condition then it will throw InvalidOperation exception. The FirstOrDefault() method does the same thing as First() method. The only difference is that it returns default value of the data type of a collection if a collection is empty or doesn't find any element that satisfies the condition.
First(): It will returns first element of sequence.But It will through an exception if the "Where Clause" value does not match or the given expression does not match.FirstOrDefault(): This is also used to returns first element of sequence But difference is that, If the "Where Clause" value does not match then it will not through an exception.
First will not manage error where if no data in results but Firstordefault manages null values if no records
b
First() - It returns first element from collection,but an exception occurred when null or Empty result return. FirstOrDefault() - It returns first element from collection or returns default element when matching condition failed.an exception not occurred when null or Empty result return.
First()- there should be only one result, an Exception is thrown if no result is returned or if it has multiple results matched.FirstOrDefault()- it returns first element of a sequence/result or a default value if no element is matched Exception not thrown it returns null instead
=>FirstOrDefault: It returns first specific element from a collection of elements if one or more than one match found for that element based on conditions in where clause if any. A default value is returned, if no match is found for that element in the collection.=>First: It returns first specific element from a collection of elements if one or more than one match found for that element based on conditions in where clause if any. An exception is thrown, if no match is found for that element in the collection.
673 down vote accepted 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 that 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).Finally, the difference between First() and Take() is that First() returns the element itself, while Take() returns a sequence of elements that contains exactly one element. (If you pass 1 as the parameter).
First() and FirstOrDefault() both are same except that one. In scenario of FirstOrDefault, if no element match the specified condition than it returns default value of underlying type of generic collection.
Using "First" it will get the exception if no records FirstOrDefault it will return header colums
https://www.c-sharpcorner.com/blogs/first-firstordefault-single-singleordefault-in-c-sharp