5

what kind of data FirstOrDefault or SingleOrDefault will return.

suppose my query return 3 record like

empid  ename  salary
-----  -----  ------
1      joy    1500
2      rob    4500
3      jen    6500

so if we use FirstOrDefault or SingleOrDefault then what kind of resultset i will get. please explain with example. thanks

agent-j
  • 27,335
  • 5
  • 52
  • 79
Mou
  • 15,673
  • 43
  • 156
  • 275

3 Answers3

35
                | 0 values    | 1 value     | > 1 value
FirstOrDefault  | Default     | First value | First value
SingleOrDefault | Default     | First value | Exception

And to extend this table to the entire set:

                | 0 values    | 1 value     | > 1 value
First           | Exception   | First value | First value
FirstOrDefault  | Default     | First value | First value
Single          | Exception   | First value | Exception
SingleOrDefault | Default     | First value | Exception
Last            | Exception   | Last value  | Last value
LastOrDefault   | Default     | Last value  | Last value

And here's another version with some concrete values to make it clearer:

                | []          | [1]         | [1,2,3]
First           | Exception   | 1           | 1
FirstOrDefault  | 0           | 1           | 1
Single          | Exception   | 1           | Exception
SingleOrDefault | 0           | 1           | Exception
Last            | Exception   | 1           | 3
LastOrDefault   | 0           | 1           | 3
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • 1
    i just could not understand at all from ur help. rather explanation would be better....thanks – Mou Jul 11 '11 at 13:59
  • 8
    @user72 : what don't you understand? The table is crystal clear. – H H Jul 11 '11 at 14:10
  • 2
    @user728750 : With your data sample, `FirstOrDefault` will give you "joy" and `SingleOrDefault` will throw an exception. – Richard Szalay Jul 11 '11 at 15:34
3

SingleOrDefault will return exception because it wait to get one record or no record and FirstOrDefault will return the first record (1 joy 1500) you can use SingleOrDefault when your where contains a condition that will surly return on record in you case - where empid == 1 obviously you want only one DB record with the ID 1

kleinohad
  • 5,800
  • 2
  • 26
  • 34
  • thanks...give me a small sample for SingleOrDefault. i need to know what kind of situation SingleOrDefault will be used. please need a sample. – Mou Jul 11 '11 at 13:58
  • you can use SingleOrDefault when your where contains a condition that will surly return on record in you case - where empid == 1 obviously you want only one DB record with the ID 1 – kleinohad Jul 11 '11 at 14:02
3

FirstOrDefault will return you the first element and the default value (default value for value type and null for reference type) the element if no elements are present.

SingleOrDefault will return the the element if only one is present. Default if none are present and throws an exception if more than one elements are in your query.

In your case FirstOrDefault will return the first element. And SingleOrDefault will throw an exception.

ghimireniraj
  • 397
  • 1
  • 5