Interviews - C# Corner

Amita Palkar - Junnarkar
Difference between FirstOrDefault and First in LINQ
By Amita Palkar - Junnarkar in LINQ onMar 23 2018
  • Aniket Rana
    Apr, 2018 3

    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.

    • 11
  • Deepak Verma
    May, 2018 18

    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.

    • 4
  • Sudarshan Hajare
    Jul, 2019 3

    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.

    • 2
  • Jignesh Kumar
    Jun, 2018 18

    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.

    • 2
  • Shrimant Telgave
    May, 2018 2

    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.

    • 2
  • SreeRaj S
    Mar, 2019 25

    b

    • 0
  • Duddu nagsd
    Oct, 2018 10

    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.

    • 0
  • Rushyanth Reddy
    Aug, 2018 6

    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

    • 0
  • Gopaiah Nekarukanti
    Jul, 2018 22

    =>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.

    • 0
  • Shivam Shukla
    Jun, 2018 27

    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).

    • 0
  • Sanjit Kumar Singh
    May, 2018 5

    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.

    • 0
  • t.santosh rao
    Apr, 2018 18

    Using "First" it will get the exception if no records FirstOrDefault it will return header colums

    • 0
  • Gajanan Chavhan
    Apr, 2018 9

    https://www.c-sharpcorner.com/blogs/first-firstordefault-single-singleordefault-in-c-sharp

    • 0
  • Gajanan Chavhan
    Apr, 2018 9

    https://www.c-sharpcorner.com/blogs/first-firstordefault-single-singleordefault-in-c-sharp

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS