Interviews - C# Corner

Nilesh Shah
What is the difference between First() and FirstOrDefault() selector methods in LINQ?
By Nilesh Shah in LINQ onFeb 01 2017
  • Nilesh Shah
    Feb, 2017 1

    First() always expects at least one element in result set, if there isn't any element is result, then First() returns an exception. While FirstOrDefault() is fine with a result set having 0 elements, it does not throw any exception

    • 10
  • sushil kumar
    Jul, 2017 14

    First() :- it always find at least one value from result set. if no value or more than one value then it will through exception. FirstOrDefault() :- return the first element from sequence or return the default value from result set. if no element is found then it will through exception.

    • 1
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 1
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 1
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 1
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 1
  • Naveen Bisht
    Feb, 2017 16

    First()- their are at least one element in sequence FirstOrDefault()- you are checking wither their is any element in sequence.

    • 1
  • Sudhir Singh
    Jun, 2018 20

    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.

    • 0
  • Nilesh Shah
    Jun, 2017 23

    @Rahul I see your multiple and same replies, keep only one and delete others

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0
  • RAHUL SHUKLA
    Jun, 2017 23

    Hi First() throw an exception when there is no record where as FirstOrDefault () does not throw an exception.So we can say First() does not support Null value where as FirstOrDefault support Null values

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS