Posts

Showing posts from April, 2023

SingleOrDefault() Vs. FirstOrDefault() in LINQ Query

  Single() / SingleOrDefault()   First () / FirstOrDefault() Single()  - There is exactly 1 result, an exception is thrown if no result is returned or more than one result.  SingleOrDefault()  – Same as Single(), but it can handle the null value. 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. Single() asserts that one and only one element exists in the sequence. First() simply gives you the first one. When to use Use Single / SingleOrDefault() when you sure there is only one record present in database or you can say if you querying on database with help of primary key of table. When to use Developer may use First () / FirstOrDefault() anywhere,  when they required single value from collection or database.

How to call Stored Procedure in Entity Framework?

 Stored Procedure as Entity Function: Step 1. Import Stored Procedure in Entity Framework Step 2. Right-click Stored Procedure and select "Add Function Import". Step 3.  Here, we can map a returned object of our Stored Procedure. Now, we can call the Stored Procedure an entity function using the following code. The entity function returns a complex type called "EmployeeDetails". Call Stored Procedure using DbDataReader