Thursday, 21 June 2018

001 LINQ introduction

The full form of LINQ is language integrated query. That means the SQL Query will be written with  programming language. In traditional programming , we write  SQL statement under stored procedure or  make a connection to the database  write SQL in between quotation marks (magic string). Both are separate SQL statement from programming language. LINQ offer SQL statement to be written within programming language , it is a part of programming language . This make development faster, easier and manageable. 

Below is example of a stored procedure
 



Below is the example SQL statement that is called from programming language.



                        public string GetSysParamData(string sys_param_code)
                        {
                                  string sys_param_value = "";
                                  SqlConnection conn = new SqlConnection(connectionString);
                                  conn.Open();
                                  SqlCommand command = new SqlCommand("SELECT sys_param_value FROM tbl_sref_sys_param WHERE sys_param_code='" + sys_param_code + "'", conn);
                                 using (SqlDataReader reader = command.ExecuteReader())
                                            {
                                                   if (reader.Read())
                                                     {
                                                   sys_param_value = String.Format("{0}", reader["sys_param_value"]);
                                                      }
                                             }
                                           conn.Close();
                                       return sys_param_value;
                            }




Below is the example of LINQ statement,  the
LINQ statement is written with c Sharp language.




protected void Page_Load(object sender, EventArgs e)
{
         List<student> pat=new List<student>();
         student s=new student();
          GridView1.DataSource = from stu in student
         where stu.Gender == "Female"
         select stu;

        GridView1.DataBind();
}


LINQ was introduced in  Visual Studio 2008 ,.Net  framework 3.5.

Architecture design by Anders Heilberd  as a part of.Net development .

The advantages  of LINQ is

  • LINQ statement is part of programming language,  that reduce development time and code written more faster.
  • Debugging is easier as LINQ statement is a part of programming language. The statement can also be debug  which was not possible for stored procedure.
  • Easy transformation data type SQL to XML.
  • Auto generated domain name object data usable for small project.
  • As LINQ is directly written with code, no extra magic string is required for SQL statement.
  • Sometimes lazy loading speed up the performance of the application.
  • LINQ reduce code, as it is does not uses stored procedure name, parameters, magic strings ect.
  • LINQ expressions are strongly typed, that prevent mismatch.
  • LINQ offers to join several data source in a single.
  • Query of LINQ is more accurate.

Namespace used for LINQ

  • System.LINQ
  • System.Collecrion.Generic
  • System.Data.LINQ
  • System.XML.LINQ
  • System.Data.LINQ.Mapping

Types of  LINQ

  • LINQ to object
  • LINQ to XML
  • LINQ to data set
  • LINQ to SQL
  • LINQ to Entity

We have discussed several advantages of LINQ. But LINQ have some disadvantage also

  • LINQ integrate another layer , which  provide an extra  overhead on the application.
  • LINQ statement can not be compiled  like  stored procedure, there is no predefined execution path  of  LINQ statement.
  • If you make a small change in LINQ statement,  you need to compile the entire solution, which is the biggest disadvantage of LINQ.
  • Performance degraded, if  not written in correct manner.

Below image show the architecture of LINQ. Application written in.Net languages,  communicate  via LINQ enable data source.






Where is the difference between stored procedure and LINQ

  • Stored procedue is faster as execution plan and  pre compile. LINQ has no execution path and pre compilation.
  • If you make changes in store procedue, you need to compile stored procedure only. If you do a small change in LINQ statement, you need to recompile all the project.
  • LINQ support multi database, but stored procedure resides in a database.
  • LINQ statement can be debug during run time as a part of. Net programming language, store procedure cannot be  debug. You have to test you store procedure with data.

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...