Monday, 8 April 2019

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.
Single() or SingleOrDefault() will generate a regular TSQL like "SELECT ...".
The First() or FirstOrDefault() method will generate the TSQL statment like "SELECT TOP 1..."
In the case of Fist / FirstOrDefault, only one row is retrieved from the database so it performs slightly better than single / SingleOrDefault. such a small difference is hardly noticeable but when table contain large number of column and row, at this time performance is noticeable.

Thursday, 4 April 2019

IQS

  1. Difference between CROSS APPLY and OUTER APPLY in SQL Server
  2. Understanding a SQL Server execution plan
  3. difference between function and stored procedure
  4. how to apply indexes in sql server
  5. how to apply cursors in sql server
  6. how to pass authentication token in header
  7. select sum(CustomerID) from Customer
  8. how to set outproc session in asp.net
  9. How to improve SQL Server database performance
    1. Improve SQL Query Performance.
    2. Avoid Multiple Joins in a Single Query. ...
    3. Eliminate Cursors from the Query. ...
    4. Avoid Use of Non-correlated Scalar Sub Query. ...
    5. Avoid Multi-statement Table Valued Functions (TVFs) ...
    6. Creation and Use of Indexes. ...
    7. Understand the Data. ...
    8. Create a Highly Selective Index.


Ø    Stored procedure can have input and output parameters.
Ø       Function allow only input parameters, doesn’t support output parameters.

Ø     Stored procedures cannot be called from Function.
Ø     Function can be called from Procedure.

Ø   Stored procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statements.           
Ø    Function allows only SELECT statement, we cannot use DML statements in Function.

Ø  Stored procedure cannot be called from Select/Where/Having and so on statements. Execute or Exec statement can be used to call or execute Stored Procedure.
Ø  Function can be called from select statement.

Ø  Exception can be handled by try-catch block in a stored procedure.
Ø  try-catch block cannot be used in a Function.

Ø  We can use transactions within stored procedure.
Ø  Transactions are not allowed within Function.

Ø  We can use table variables as well as temporary tables in Procedure.
Ø  We can use only table variables in Function, temporary tables are not allowed.