Wednesday, August 25, 2010

LINQ - What is LINQ

What is LINQ?

LINQ (Language Integrated Query) is the most important new feature of C# 3.0 and .NET 3.5.
LINQ integrated query syntax inside the C# programming language and makes it is possible to access diffferent data source with the same syntax.

Advantage of LINQ

The code size: There are many occasions that the users have to write long sentences for getting a SQL query. LINQ provides relatively short codes in such advanced occasions. It reduces the complexity of the code and makes it much easy for the program to read.



Code equality: One of the most advantages in using LINQ is that its availability over any .NET platform language such as C#.net, VB.NET.

Sample Example on LINQ

Step 1
First create a web project.

Step 2
First Create a student class,it is look like this


   1:   /// <summary>
   2:      /// Store the Student Data
   3:      /// </summary>
   4:      public class Student
   5:      {
   6:          #region Property
   7:          /// <summary>
   8:          /// Get and Set the Student ID
   9:          /// </summary>
  10:          public int StudentID
  11:          {
  12:              get;
  13:              set;
  14:          }
  15:   
  16:          /// <summary>
  17:          /// get and set the Student First Name
  18:          /// </summary>
  19:          public String FirstName
  20:          {
  21:              get;
  22:              set;
  23:          }
  24:   
  25:          /// <summary>
  26:          /// get and set the student Last name
  27:          /// </summary>
  28:          public String LastName
  29:          {
  30:              get;
  31:              set;
  32:          }
  33:   
  34:   
  35:          #endregion
  36:      }
this class used for stored data in student object

Step 3
In default class [default.aspx.cs] write a method for store the student data which return Student list.it is look like this
in this code i intiliaze student object with following data

   1:   /// <summary>
   2:      /// Add the Student Data and Store data in list
   3:      /// </summary>
   4:      /// <returns>List</returns>
   5:      private List<Student> AddData()
   6:      {
   7:          try
   8:          {
   9:              List<Student> ListObj = new List<Student>();
  10:   
  11:              Student Std1 = new Student();
  12:              Std1.StudentID = 1;   
  13:              Std1.FirstName = "kishor";
  14:              Std1.LastName = "Naik";
  15:   
  16:   
  17:              Student Std2 = new Student();
  18:              Std2.StudentID = 2;
  19:              Std2.FirstName = "kedar";
  20:              Std2.LastName = "deshpande";
  21:   
  22:   
  23:              Student Std3 = new Student();
  24:              Std3.StudentID = 3;
  25:              Std3.FirstName = "avinash";
  26:              Std3.LastName = "k";
  27:   
  28:   
  29:              Student Std4 = new Student();
  30:              Std4.StudentID = 4;
  31:              Std4.FirstName = "sanket";
  32:              Std4.LastName = "C";
  33:   
  34:   
  35:              Student Std5 = new Student();
  36:              Std5.StudentID = 5;
  37:              Std5.FirstName = "Ramdas";
  38:              Std5.LastName = "B";
  39:   
  40:              Student Std6 = new Student();
  41:              Std6.StudentID = 6;
  42:              Std6.FirstName = "bhavesh";
  43:              Std6.LastName = "pisat";
  44:   
  45:              ListObj.Add(Std1);
  46:              ListObj.Add(Std2);
  47:              ListObj.Add(Std3);
  48:              ListObj.Add(Std4);
  49:              ListObj.Add(Std5);
  50:              ListObj.Add(Std6);
  51:   
  52:              return ListObj; 
  53:          }
  54:          catch (Exception ex)
  55:          {
  56:              throw new Exception(ex.Message); 
  57:          }
  58:      }

Step 4
Create a LinqDemo class for fetching data from Student ListObject,it is look like this
   1:     public static class LinqDemo
   2:      {
   3:   
   4:          #region Methods
   5:          /// <summary>
   6:          /// Get The Student Data with return list Object
   7:          /// </summary>
   8:          /// <param name="ListObj">Specify the List object</param>
   9:          /// <returns>List</Student></returns>
  10:          public static List<Student> GetStudentDataList(List<Student> ListObj)
  11:          {
  12:              try
  13:              {
  14:                  var Query = from Q in ListObj
  15:                              select Q;
  16:   
  17:                  return Query.ToList<Student>();
  18:              }
  19:              catch (Exception ex)
  20:              {
  21:                  throw new Exception(ex.Message); 
  22:              }
  23:          }
  24:   
  25:          /// <summary>
  26:          /// Get The Student Data with return Ilist
  27:          /// </summary>
  28:          /// <param name="ListObj">Specify the List object</param>
  29:          /// <returns>Ilist</returns>
  30:          public static IList GetStudentDataIList(List<Student> ListObj)
  31:          {
  32:              try
  33:              {
  34:                  var Query=from Q in ListObj
  35:                            select Q;
  36:   
  37:                  return (IList)Query.ToList<Student>();   
  38:              }
  39:              catch (Exception ex)
  40:              {
  41:                  throw new Exception(ex.Message);
  42:              }
  43:          }
  44:          #endregion
  45:          /// <summary>
  46:          /// Get The Student Data with return IQueryable
  47:          /// </summary>
  48:          /// <param name="ListObj">Specify the List object</param>
  49:          /// <returns>IQueryable</returns>
  50:          public static IQueryable GetStudentDataIQueryable(List<Student> ListObj)
  51:          {
  52:              try
  53:              {
  54:                  var Query = from Q in ListObj
  55:                              select Q;
  56:   
  57:                  return Query.AsQueryable();  
  58:              }
  59:              catch (Exception ex)
  60:              {
  61:                  throw new Exception(ex.Message); 
  62:              }
  63:          }
  64:   
  65:          /// <summary>
  66:          /// Get The Student Data with return IEnumerable
  67:          /// </summary>
  68:          /// <param name="Listobj">Specify the List object</param>
  69:          /// <returns>IEnumerable</returns>
  70:          public static IEnumerable<Student> GetStudentDataIEnumerable(List<Student> Listobj)
  71:          {
  72:              try
  73:              {
  74:                  var Query = from Q in Listobj
  75:                              select Q;
  76:   
  77:                  return Query.AsEnumerable<Student>();   
  78:              }
  79:              catch (Exception ex)
  80:              {
  81:                  throw new Exception(ex.Message); 
  82:              }
  83:          }
  84:      }

Step 5
In default class [default.aspx.cs] write a method for binding data to the gridview,it is look like this


   1:   protected void Page_Load(object sender, EventArgs e)
   2:      {
   3:          try
   4:          {
   5:              if (IsPostBack == false)
   6:              {
   7:                  BindData(); 
   8:              }
   9:          }
  10:          catch (Exception)
  11:          { }
  12:      }
  13:     
  14:    private void BindData()
  15:      {
  16:          try
  17:          {
  18:              /// return with List
  19:   
  20:              //gvStudent.DataSource = LinqDemo.GetStudentDataList(AddData());
  21:              //gvStudent.DataBind();
  22:   
  23:              /// return with Ilist
  24:              //gvStudent.DataSource = LinqDemo.GetStudentDataIList(AddData());
  25:              //gvStudent.DataBind(); 
  26:   
  27:              /// return with IQuerable
  28:              //gvStudent.DataSource = LinqDemo.GetStudentDataIQueryable(AddData());
  29:              //gvStudent.DataBind();
  30:   
  31:              /// return with IEnumerable
  32:              gvStudent.DataSource = LinqDemo.GetStudentDataIEnumerable(AddData());
  33:              gvStudent.DataBind();   
  34:          }
  35:          catch (Exception)
  36:          { }
  37:      }
  38:   
  39:    

Final Code
1. default.aspx

   1:  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml">
   6:  <head runat="server">
   7:      <title>Untitled Page</title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:ScriptManager ID="ScriptManager1" runat="server">
  13:          </asp:ScriptManager>
  14:          
  15:          <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  16:              <ContentTemplate>
  17:                  <asp:GridView ID="gvStudent" runat="server">
  18:                  </asp:GridView>
  19:              </ContentTemplate>
  20:          </asp:UpdatePanel>
  21:      </div>
  22:      </form>
  23:  </body>
  24:  </html>

2.Default.aspx.cs
   1:  using System;
   2:  using System.Configuration;
   3:  using System.Data;
   4:  using System.Linq;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.Web.UI;
   8:  using System.Web.UI.HtmlControls;
   9:  using System.Web.UI.WebControls;
  10:  using System.Web.UI.WebControls.WebParts;
  11:  using System.Xml.Linq;
  12:  using System.Collections;
  13:  using System.Collections.Generic;
  14:  using System.Runtime.Serialization;
  15:   
  16:  public partial class _Default : System.Web.UI.Page 
  17:  {
  18:      protected void Page_Load(object sender, EventArgs e)
  19:      {
  20:          try
  21:          {
  22:              if (IsPostBack == false)
  23:              {
  24:                  BindData(); 
  25:              }
  26:          }
  27:          catch (Exception)
  28:          { }
  29:      }
  30:   
  31:      #region Method
  32:      /// <summary>
  33:      /// Add the Student Data and Store data in list
  34:      /// </summary>
  35:      /// <returns>List</returns>
  36:      private List<Student> AddData()
  37:      {
  38:          try
  39:          {
  40:              List<Student> ListObj = new List<Student>();
  41:   
  42:              Student Std1 = new Student();
  43:              Std1.StudentID = 1;   
  44:              Std1.FirstName = "kishor";
  45:              Std1.LastName = "Naik";
  46:   
  47:   
  48:              Student Std2 = new Student();
  49:              Std2.StudentID = 2;
  50:              Std2.FirstName = "kedar";
  51:              Std2.LastName = "deshpande";
  52:   
  53:   
  54:              Student Std3 = new Student();
  55:              Std3.StudentID = 3;
  56:              Std3.FirstName = "avinash";
  57:              Std3.LastName = "k";
  58:   
  59:   
  60:              Student Std4 = new Student();
  61:              Std4.StudentID = 4;
  62:              Std4.FirstName = "sanket";
  63:              Std4.LastName = "C";
  64:   
  65:   
  66:              Student Std5 = new Student();
  67:              Std5.StudentID = 5;
  68:              Std5.FirstName = "Ramdas";
  69:              Std5.LastName = "B";
  70:   
  71:              Student Std6 = new Student();
  72:              Std6.StudentID = 6;
  73:              Std6.FirstName = "bhavesh";
  74:              Std6.LastName = "pisat";
  75:   
  76:              ListObj.Add(Std1);
  77:              ListObj.Add(Std2);
  78:              ListObj.Add(Std3);
  79:              ListObj.Add(Std4);
  80:              ListObj.Add(Std5);
  81:              ListObj.Add(Std6);
  82:   
  83:              return ListObj; 
  84:          }
  85:          catch (Exception ex)
  86:          {
  87:              throw new Exception(ex.Message); 
  88:          }
  89:      }
  90:   
  91:      /// <summary>
  92:      /// Bind the Data GridView
  93:      /// </summary>
  94:      private void BindData()
  95:      {
  96:          try
  97:          {
  98:              /// return with List
  99:   
 100:              //gvStudent.DataSource = LinqDemo.GetStudentDataList(AddData());
 101:              //gvStudent.DataBind();
 102:   
 103:              /// return with Ilist
 104:              //gvStudent.DataSource = LinqDemo.GetStudentDataIList(AddData());
 105:              //gvStudent.DataBind(); 
 106:   
 107:              /// return with IQuerable
 108:              //gvStudent.DataSource = LinqDemo.GetStudentDataIQueryable(AddData());
 109:              //gvStudent.DataBind();
 110:   
 111:              /// return with IEnumerable
 112:              gvStudent.DataSource = LinqDemo.GetStudentDataIEnumerable(AddData());
 113:              gvStudent.DataBind();   
 114:          }
 115:          catch (Exception)
 116:          { }
 117:      }
 118:      #endregion
 119:   
 120:      /// <summary>
 121:      /// Store the Student Data
 122:      /// </summary>
 123:      public class Student
 124:      {
 125:          #region Property
 126:          /// <summary>
 127:          /// Get and Set the Student ID
 128:          /// </summary>
 129:          public int StudentID
 130:          {
 131:              get;
 132:              set;
 133:          }
 134:   
 135:          /// <summary>
 136:          /// get and set the Student First Name
 137:          /// </summary>
 138:          public String FirstName
 139:          {
 140:              get;
 141:              set;
 142:          }
 143:   
 144:          /// <summary>
 145:          /// get and set the student Last name
 146:          /// </summary>
 147:          public String LastName
 148:          {
 149:              get;
 150:              set;
 151:          }
 152:   
 153:   
 154:          #endregion
 155:      }
 156:   
 157:      public static class LinqDemo
 158:      {
 159:   
 160:          #region Methods
 161:          /// <summary>
 162:          /// Get The Student Data with return list Object
 163:          /// </summary>
 164:          /// <param name="ListObj">Specify the List object</param>
 165:          /// <returns>List</Student></returns>
 166:          public static List<Student> GetStudentDataList(List<Student> ListObj)
 167:          {
 168:              try
 169:              {
 170:                  var Query = from Q in ListObj
 171:                              select Q;
 172:   
 173:                  return Query.ToList<Student>();
 174:              }
 175:              catch (Exception ex)
 176:              {
 177:                  throw new Exception(ex.Message); 
 178:              }
 179:          }
 180:   
 181:          /// <summary>
 182:          /// Get The Student Data with return Ilist
 183:          /// </summary>
 184:          /// <param name="ListObj">Specify the List object</param>
 185:          /// <returns>Ilist</returns>
 186:          public static IList GetStudentDataIList(List<Student> ListObj)
 187:          {
 188:              try
 189:              {
 190:                  var Query=from Q in ListObj
 191:                            select Q;
 192:   
 193:                  return (IList)Query.ToList<Student>();   
 194:              }
 195:              catch (Exception ex)
 196:              {
 197:                  throw new Exception(ex.Message);
 198:              }
 199:          }
 200:          #endregion
 201:          /// <summary>
 202:          /// Get The Student Data with return IQueryable
 203:          /// </summary>
 204:          /// <param name="ListObj">Specify the List object</param>
 205:          /// <returns>IQueryable</returns>
 206:          public static IQueryable GetStudentDataIQueryable(List<Student> ListObj)
 207:          {
 208:              try
 209:              {
 210:                  var Query = from Q in ListObj
 211:                              select Q;
 212:   
 213:                  return Query.AsQueryable();  
 214:              }
 215:              catch (Exception ex)
 216:              {
 217:                  throw new Exception(ex.Message); 
 218:              }
 219:          }
 220:   
 221:          /// <summary>
 222:          /// Get The Student Data with return IEnumerable
 223:          /// </summary>
 224:          /// <param name="Listobj">Specify the List object</param>
 225:          /// <returns>IEnumerable</returns>
 226:          public static IEnumerable<Student> GetStudentDataIEnumerable(List<Student> Listobj)
 227:          {
 228:              try
 229:              {
 230:                  var Query = from Q in Listobj
 231:                              select Q;
 232:   
 233:                  return Query.AsEnumerable<Student>();   
 234:              }
 235:              catch (Exception ex)
 236:              {
 237:                  throw new Exception(ex.Message); 
 238:              }
 239:          }
 240:      }
 241:  }

2 comments: