Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
AJAX
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
general knowledge
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query
Question Posted on 29 Apr 2020

Home >> DotNet >> LINQ >> Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query




Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query
Below are the 3 main difference between Single() vs SingleOrDefault() vs First() vs FirstOrDefault()

Defination


(1)Single():- This will return a single specific element froma sequence
(2)SingleOrDefault():-This will return the single specific element from sequence or will return default value if that element not found
(3)First():-This will return the first element from sequence
(4)FirstOrDefault():- This will returns the first element of a sequence or default value if no element is found

Exception thrown


(1)Single():-It will throws error when 0 or more then 1 elements comes in result
(2)SingleOrDefault():-It will throws error if more then 1 element in result
(3)First():-This will throws error if no elements in the result is return
(4)FirstOrDefault():-If source is null then it will throws error

When to Use


(1)Single():-It will be used when we exactly have 1 element expected and value is not 0 or more then 1
(2)SingleOrDefault():-When we expect 0 or 1 element then we use it
(3)First():-When we expect more then 1 element and we need only first value
(4)FirstOrDefault():-When more then 1 element expected and we need only the first element. It is also okay when result is empty
0
0



 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


Other Important Questions
Important aspects of Query Operators in LINQ?
How to sort array using LINQ
Get multiple of 3 from given array with LINQ
Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query
What are the different LINQ provide






 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |