crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Single() vs SingleOrDefault() vs First() vs FirstOrDefault() in LINQ Query
Question Posted on 29 Apr 2020Home >> 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



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




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






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap