LINQ - डेटासेट

डेटासैट मेमोरी में एक अत्यंत उपयोगी डेटा प्रतिनिधित्व प्रदान करता है और इसका उपयोग विभिन्न डेटा आधारित अनुप्रयोगों के लिए किया जाता है। LINQ to Dataset में से एक के रूप में LINQ से ADO.NET की तकनीक एक डेटासेट के डेटा पर परेशानी रहित तरीके से कार्य करने की सुविधा प्रदान करती है और उत्पादकता को बढ़ाती है।

LINQ To Dataset का परिचय

LINQ to Dataset ने डेवलपर्स के लिए क्वेरी को सरल बनाने का काम किया है। उन्हें एक विशिष्ट क्वेरी भाषा में प्रश्नों को लिखने की आवश्यकता नहीं है, इसके बजाय प्रोग्रामिंग भाषा में लिखा जा सकता है। LINQ से डेटासेट क्वेरी के लिए भी उपयोगी है जहाँ डेटा को कई डेटा स्रोतों से समेकित किया जाता है। मेमोरी कलेक्शंस से डेटा एक्सेस करने के लिए भी LINQ से SQL और LINQ की तरह किसी LINQ प्रदाता की जरूरत नहीं है।

नीचे डेटासेट क्वेरी के लिए एक LINQ का एक सरल उदाहरण है जिसमें एक डेटा स्रोत पहले प्राप्त किया जाता है और फिर डेटासेट दो डेटा तालिकाओं से भर जाता है। दोनों तालिकाओं के बीच एक संबंध स्थापित किया जाता है और एक खंड के रूप में दोनों तालिकाओं के खिलाफ एक LINQ क्वेरी बनाई जाती है। अंत में, वांछित परिणाम प्रदर्शित करने के लिए foreach loop का उपयोग किया जाता है।

सी#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQtoDataset {
   class Program {
      static void Main(string[] args) {
      
         string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToString();

         string sqlSelect = "SELECT * FROM Department;" + "SELECT * FROM Employee;";

         // Create the data adapter to retrieve data from the database
         SqlDataAdapter da = new SqlDataAdapter(sqlSelect, connectString);
        
         // Create table mappings
         da.TableMappings.Add("Table", "Department");
         da.TableMappings.Add("Table1", "Employee");

         // Create and fill the DataSet
         DataSet ds = new DataSet();
         da.Fill(ds);

         DataRelation dr = ds.Relations.Add("FK_Employee_Department",
                           ds.Tables["Department"].Columns["DepartmentId"],
                           ds.Tables["Employee"].Columns["DepartmentId"]);

         DataTable department = ds.Tables["Department"];
         DataTable employee = ds.Tables["Employee"];

         var query = from d in department.AsEnumerable()
                     join e in employee.AsEnumerable()
                     on d.Field<int>("DepartmentId") equals
                     e.Field<int>("DepartmentId")                        
                     select new {
                        EmployeeId = e.Field<int>("EmployeeId"),
                        Name = e.Field<string>("Name"),                            
                        DepartmentId = d.Field<int>("DepartmentId"),                            
                        DepartmentName = d.Field<string>("Name")
                     };

         foreach (var q in query) {
            Console.WriteLine("Employee Id = {0} , Name = {1} , Department Name = {2}",
               q.EmployeeId, q.Name, q.DepartmentName);
         }

         Console.WriteLine("\nPress any key to continue.");
         Console.ReadKey();
      }
   }
}

वीबी

Imports System.Data.SqlClient
Imports System.Linq

Module LinqToDataSet

   Sub Main()
   
      Dim connectString As String = System.Configuration.ConfigurationManager.ConnectionStrings("LinqToSQLDBConnectionString").ToString()

      Dim sqlSelect As String = "SELECT * FROM Department;" + "SELECT * FROM Employee;"
      Dim sqlCnn As SqlConnection = New SqlConnection(connectString)
      sqlCnn.Open()

      Dim da As New SqlDataAdapter
      da.SelectCommand = New SqlCommand(sqlSelect, sqlCnn)

      da.TableMappings.Add("Table", "Department")
      da.TableMappings.Add("Table1", "Employee")

      Dim ds As New DataSet()
      da.Fill(ds)

      Dim dr As DataRelation = ds.Relations.Add("FK_Employee_Department", ds.Tables("Department").Columns("DepartmentId"), ds.Tables("Employee").Columns("DepartmentId"))

      Dim department As DataTable = ds.Tables("Department")
      Dim employee As DataTable = ds.Tables("Employee")

      Dim query = From d In department.AsEnumerable()
                  Join e In employee.AsEnumerable() On d.Field(Of Integer)("DepartmentId") Equals
                  e.Field(Of Integer)("DepartmentId")
                  Select New Person With { _
                        .EmployeeId = e.Field(Of Integer)("EmployeeId"),
                        .EmployeeName = e.Field(Of String)("Name"),
                        .DepartmentId = d.Field(Of Integer)("DepartmentId"),
                        .DepartmentName = d.Field(Of String)("Name")
                  }

      For Each e In query
         Console.WriteLine("Employee Id = {0} , Name = {1} , Department Name = {2}", e.EmployeeId, e.EmployeeName, e.DepartmentName)
      Next

      Console.WriteLine(vbLf & "Press any key to continue.")
      Console.ReadKey()
	  
   End Sub
  
   Class Person
      Public Property EmployeeId As Integer
      Public Property EmployeeName As String
      Public Property DepartmentId As Integer
      Public Property DepartmentName As String
   End Class
   
End Module

जब C # या VB के उपरोक्त कोड को संकलित और निष्पादित किया जाता है, तो यह निम्नलिखित परिणाम उत्पन्न करता है -

Employee Id = 1, Name = William, Department Name = Account
Employee Id = 2, Name = Benjamin, Department Name = Account
Employee Id = 3, Name = Miley, Department Name = Sales

Press any key to continue.

Dataset को LinQ का उपयोग करके डेटासेट क्वेरी करना

डेटासेट में LINQ का उपयोग करके डेटासेट की क्वेरी करने से पहले डेटा को डेटासेट में लोड करना महत्वपूर्ण है और यह DataAdapter वर्ग या LINQ से SQL का उपयोग करके किया जाता है। LINQ से डेटासेट में LINQ का उपयोग करके प्रश्नों का निरूपण, अन्य LINQ सक्षम डेटा स्रोतों के साथ LINQ का उपयोग करके प्रश्नों को तैयार करने के समान है।

एकल-तालिका क्वेरी

निम्न एकल-तालिका क्वेरी में, सभी ऑनलाइन ऑर्डर SalesOrderHeaderTtable से एकत्र किए जाते हैं और फिर ऑर्डर आईडी, ऑर्डर तिथि के साथ-साथ ऑर्डर संख्या को आउटपुट के रूप में प्रदर्शित किया जाता है।

C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqToDataset {
   class SingleTable {
      static void Main(string[] args) {
      
         string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToString();

         string sqlSelect = "SELECT * FROM Department;";

         // Create the data adapter to retrieve data from the database
         SqlDataAdapter da = new SqlDataAdapter(sqlSelect, connectString);

         // Create table mappings
         da.TableMappings.Add("Table", "Department");           

         // Create and fill the DataSet
         DataSet ds = new DataSet();
         da.Fill(ds);

         DataTable department = ds.Tables["Department"];            

         var query = from d in department.AsEnumerable()                        
         select new {
            DepartmentId = d.Field<int>("DepartmentId"),
            DepartmentName = d.Field<string>("Name")
         };

         foreach (var q in query) {
            Console.WriteLine("Department Id = {0} , Name = {1}",
               q.DepartmentId, q.DepartmentName);
         }

         Console.WriteLine("\nPress any key to continue.");
         Console.ReadKey();
      }
   }
}

VB

Imports System.Data.SqlClient
Imports System.Linq

Module LinqToDataSet

   Sub Main()
   
      Dim connectString As String = System.Configuration.ConfigurationManager.ConnectionStrings("LinqToSQLDBConnectionString").ToString()

      Dim sqlSelect As String = "SELECT * FROM Department;"
      Dim sqlCnn As SqlConnection = New SqlConnection(connectString)
      sqlCnn.Open()

      Dim da As New SqlDataAdapter
      da.SelectCommand = New SqlCommand(sqlSelect, sqlCnn)

      da.TableMappings.Add("Table", "Department")
      Dim ds As New DataSet()
      da.Fill(ds)

      Dim department As DataTable = ds.Tables("Department")

      Dim query = From d In department.AsEnumerable()
      Select New DepartmentDetail With {
         .DepartmentId = d.Field(Of Integer)("DepartmentId"),
            .DepartmentName = d.Field(Of String)("Name")
      }

      For Each e In query
         Console.WriteLine("Department Id = {0} , Name = {1}", e.DepartmentId, e.DepartmentName)
      Next

      Console.WriteLine(vbLf & "Press any key to continue.")
      Console.ReadKey()
   End Sub

   Public Class DepartmentDetail
      Public Property DepartmentId As Integer
      Public Property DepartmentName As String
   End Class
   
End Module

जब C # या VB के उपरोक्त कोड को संकलित और निष्पादित किया जाता है, तो यह निम्नलिखित परिणाम उत्पन्न करता है -

Department Id = 1, Name = Account
Department Id = 2, Name = Sales
Department Id = 3, Name = Pre-Sales
Department Id = 4, Name = Marketing

Press any key to continue.