Tuesday, 3 July 2018

002 LINQ Environment

          LINQ can be written in all language of Visual Studio .But syntax are different for languages different.LINQ is available from Visual Studio 2010 and (.Net Framework 3.5) and upper version. Now , we will learn how to create a setup LINQ environment. 

           To do this , you have to install Visual Studio first. There are two options, download Visual Studio Express  or purchase license version of Visual Studio for professional uses. In the example we have downloaded Visual Studio 2015 . First  we need to click on setup, the setup will  start. 



Installation has two option "default" or "customs" , you need to choose default options, Visual Studio will proceed, progress bar will show the progress of installation. 



After that you will see a message that  the installation is completed. If you have downloaded Express version, you need to sign up Microsoft to smooth  performance of Visual Studio.


To  create  a console application 


click  1)File   --> New Project 
       2)Choose --> Console Application




you will get this editor. Please keep in mind, LINQ can be used  for desktop application, web application , mobile application also. For the purpose of training we have chosen console application.




Below is the example of LINQ. There are three example. The first example is simple LINQ query  over an array. The second example of utility of IEnumerable interface.  IEnumerableis heart of LINQ queries, you will learn details it in the letter chapter.The third  example is how complex  object is handle by LINQ queries.


Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
using System.Collections;
namespace ConsoleApplication1
{
      class Program
     {
        static void Main(string[] args)
          {
                         string[] country = { "India", "Brazil", "China", "USA", "Pakistan", "South Afrika", "Japan", "Russia", "Poland", "Sweden" };

 
             /* Example 1 (Simple Select Statement) */
                      var AllCountry = from c in country select c;


                     Console.WriteLine("List of Country");
                     Console.WriteLine("----------------");
 
            foreach (var c in AllCountry)
                    {
                              Console.WriteLine(c);
                      }
                       Console.ReadLine();

                      /* Example 1 (All output as reverse list) */
                     var uppercseCountry = country.Reverse().ToArray();
                    Console.WriteLine("Revese List of Country");
                    Console.WriteLine("----------------");

                     foreach (var c in uppercseCountry)
                     {
                               Console.WriteLine(c);
                      }
                     Console.ReadLine();


                     /* Example 3 (LINQ on Array List) */
                      ArrayList myList = new ArrayList();
                     myList.Add(
                     new student
                      {
                          name = "John",
                          roll = 12,
                           age = 9
                         });
                        myList.Add(
                         new student
                       {
                            name = "John1",
                            roll = 14,
                             age = 8
                        });
                        myList.Add(
                        new student
                        {
                          name = "John2",
                          roll = 13,
                          age = 7
                           });
                         myList.Add(
                         new student
                         {
                                name = "John3",
                                roll = 15,
                                 age = 8
                          });

                        var query = from student s in myList
                                          where s.age > 7
                                          select s;
                       Console.WriteLine("List of Student Over 7 Years of Age");
                        Console.WriteLine("--------------------------------------");


                         foreach (student c in query)
                        {
                                      Console.WriteLine("Name : "+c.name);
                                       Console.WriteLine("Roll : " + c.roll);
                                       Console.WriteLine("Age : " + c.age);
                                       Console.WriteLine("\n");
             }
                        Console.ReadLine();
                        }
                   }
}
public class student
{
                 public string name { get; set; }
                 public int roll { get; set; }
                  public int age { get; set; }
}



After you have written the code, press F5  run the code. You will get the output one by one  as you press the key.


Output :
 
List of Country
----------------
India
Brazil
China
USA
Pakistan
South Afrika
Japan
Russia
Poland
Sweden


Revese List of Country
----------------
Sweden
Poland
Russia
Japan
South Afrika
Pakistan
USA
China
Brazil
India


List of Student Over 7 Years of Age
--------------------------------------
Name : John
Roll : 12
Age : 9




Name : John1
Roll : 14
Age : 8




Name : John3
Roll : 15
Age : 8





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.

Thursday, 14 June 2018

026 BackboneJS Backbone.noConflict

Backbone.noConflict

Get the Backbone object back to its original value.  
 
Syntax : var backbone = Backbone.noConflict()




<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
</head>
<script type="text/javascript">

Backbone.emulateHTTP = true;

Backbone.sync = function (method, model)
{
document.write(method + ": " + JSON.stringify(model) + "");
model.set('id', 1);
};

var student = new Backbone.Model
({
name: "john",
class: 4
});

student.save();
student.set({ name: "john2" });
student.save();
student.destroy();

var noConflikObj = Backbone.noConflict();

document.write(noConflikObj.VERSION + "");
</script>


Output :


create: {"name":"john","class":4}
update: {"name":"john2","class":4,"id":1}
delete: {"name":"john2","class":4,"id":1}
1.1.2



025 BackboneJS Backbone.emulateJSON

Backbone.emulateJSON
 

If your browser is legacy and can't handle requests encoded as application/json.Backbone.emulateJSON = true will serialized under a model parameter.

Syntax : Backbone.emulateJSON = true
 
Example



<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
</head>
<script type="text/javascript">

Backbone.emulateHTTP = true;

Backbone.sync = function (method, model)
{
document.write(method + ": " + JSON.stringify(model) + "");
model.set('id', 1);
};

var student = new Backbone.Model
({
name: "john",
class: 4
});

student.save();
student.set({ name: "john2" });
student.save();
student.destroy();
</script>

Output :

create: {"name":"john","class":4}
update: {"name":"john2","class":4,"id":1}
delete: {"name":"john2","class":4,"id":1} 

024 BackboneJS Backbone.emulateHTTP

Backbone.emulateHTTP
 
If you server does not support REST/HTTP , your server is legacy , then you must use Backbone.emulateHTTP = true


Example


<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
</head>
<script type="text/javascript">

Backbone.emulateHTTP = true;

Backbone.sync = function (method, model)
{
document.write(method + ": " + JSON.stringify(model) + "");
model.set('id', 1);
};

var student = new Backbone.Model
({
name: "john",
class: 4
});

student.save();
student.set({ name: "john2" });
student.save();
student.destroy();
</script>


Output :

create: {"name":"john","class":4}
update: {"name":"john2","class":4,"id":1}
delete: {"name":"john2","class":4,"id":1}


023 BackboneJS Backbone.ajax

Backbone.ajax

You can use a custom AJAX function, if your endpoint do not support the jQuery.ajax API.


Example :


<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
</head>
<script type="text/javascript">
Backbone.ajax({
dataType: "jsonp",
url: "https://mysite.com/abcd.html?id=2",
data: "",
success: function (val) {
var myModel = new Backbone.Model
({
name: val
});

var myCollection = Backbone.Collection.extend({
model: myModel
});
collection = new myCollection(val);
}
});
</script>



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

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