Introduction to LINQ c# In Hindi

Introduction to LINQ c# In Hindi

C# प्रोग्रामिंग लैंग्वेज में LINQ (लैंग्वेज इंटीग्रेटेड क्वेरी) C# प्रोग्राम में यूजर डिफाइन मल्टीप्ल क्लास मेथड्स का एक ग्रुप कलेक्शन स्टोरेज है. जो किसी C# प्रोग्राम में मल्टीप्ल टाइप  के क्लास डेटा सोर्स (जैसे ऐरे डाटा सीक्वेंस, कलेक्शन डाटा टाइप, डेटाबेस एलिमेंट, XML फाइल फॉर्मेट, आदि) डाटा टाइप कलेक्शन को डिक्लेरेटिव स्टाइल फॉर्मेट में प्रोग्राम के माध्यम से डायरेक्ट क्वेरी करने का एक यूनिफाइड मेथड प्रोवाइड करवाता है। यह C# यूजर को प्रोग्राम सोर्स कोड में डायरेक्ट SQL जैसी डेटाबेस सॉफ्टवेयर क्वेरी क्रिएट करने की परमिशन प्रोवाइड करता है. जिससे की मौजूदा प्रोग्राम में डेटा क्वेरी रिट्रीवल मैनिपुलेशन प्रोसेस अधिक इजी और स्माल हो जाता है।

Introduction to LINQ c# In Hindi

LINQ मेथड C# प्रोग्राम में मल्टीप्ल यूजर डिफाइन क्लास डेटा टाइप में एक जैसा सिंटैक्स अप्लाई करने में हेल्प करके डेटाबेस क्वेरी डेटा के साथ काम करने के प्रोसेस को इजी करता है। यह डेटाबेस से क्वेरी करने की कैपेबिलिटी को इमीडियेट डायरेक्ट C# लैंग्वेज में इंजेक्ट या इंटीग्रेट करता है. जिससे C# प्रोग्राम सोर्स कोड को रीड और मेंटेन करने की कैपेसिटी इम्प्रूव होती है।

Advanced Features of LINQ in C# Programming.

  • Query Syntax and Method Syntax – C# प्रोग्राम में LINQ क्वेरी को मुख्य रूप से दो सिंटैक्स में क्रिएट किया जा सकता है. जिसमे एक क्वेरी सिंटैक्स (SQL सिंटेक्स जैसा) और दूसरा मेथड सिंटैक्स (फ्लूएंट-स्टाइल मेथड) जैसा, आप दोनों में कोई एक यूज़ कर सकते है।
  • Deferred Execution – C# प्रोग्राम में LINQ क्वेरी डाटा तब एग्जीक्यूट नहीं होता है, जब उन्हें प्रोग्राम में डिफाइन किया जाता है. बल्कि यह तब एक्सेक्यूट होती हैं. जब प्रोग्राम आउटपुट रिज़ल्ट एक्चुअल में एन्यूमरेट किए जाते हैं। इसका अर्थ है कि link डेटाबेस क्वेरी तभी एग्जीक्यूट होगी, जब मौजूदा प्रोग्राम में एक्चुअल डेटा की आवश्यकता होगी। जिससे की डेटाबेस की परफॉर्मेंस इम्प्रूव हो सकती है।
  • Strongly Typed – C# प्रोग्राम में डेटाबेस इंटीग्रेशन के लिए यूज़ LINQ क्वेरीज़ टाइप-सेफ प्रोसेस होती हैं, इसका मतलब है कि C# प्रोग्राम कंपाइलर यह फिक्स करता है कि आप आवश्यक प्रोग्राम डेटा को उसके डाटा टाइप के अनुसार प्रॉपर आर्डर में क्वेरी कर रहे हैं।
  • Extensibility – किसी भी C# प्रोग्राम में LINQ डाटा क्वेरी को अलग-अलग डेटा सोर्स पर यूज़ या अप्लाई किया जाता है. जैसे कि ऐरे डाटा टाइप, कलेक्शन डाटा, XML डाटा फॉर्मेट, डेटाबेस (एंटिटी फ्रेमवर्क या LINQ to SQL का यूज़ करके), इसके अलावा और भी बहुत इसे जरूरत पड़ने पर एक्सटेंड किया जा  सकता है।

Basic Syntax of LINQ in C#.

LINQ Query Syntax in C#.

LINQ का क्वेरी सिंटैक्स SQL ​​डेटाबेस सॉफ्टवेयर सिंटेक्स के जैसा ही होता है, और इसमें SQL के समान ही from, where, select, group by, आदि रिजर्व्ड डेटाबेस कीवर्ड्स स्टेटमेंट का यूज़ किया जाता है।

var output = from data in collection

where data.Condition

select data;

Method Query Syntax in C#.

C# प्रोग्राम link क्वेरी इंटीग्रेशन मेथड में सिंटैक्स ज़्यादा इजी स्टाइल का यूज़ किया जाता है, और डेटाबेस क्वेरी रिट्रीवल LINQ एक्सटेंशन मेथड्स में जैसे, Where(), Select(), OrderBy(), आदि फंक्शन sql कीवर्ड स्टेटमेंट बेस्ड होते है।

var output = collection.Where(data => data.Condition)

.Select(data => data.Property);

Querying LINQ to Objects data.

C# प्रोग्राम में LINQ मेथड का सबसे कॉमन यूज़ इन-मेमोरी कलेक्शन जैसे ऐरे, लिस्ट, और डिक्शनरी डेटाबेस से डाटा इनफार्मेशन क्वेरी करना है। यहाँ इसमें इंडीकेट किया गया है कि LINQ डाटा कनेक्शन मेथड इन कलेक्शन के साथ कैसे परफॉर्म करता है।

Example of using the LINQ connection method with the array data type.

using System;

using System.Linq;

public class Program

{

    public static void Main()

    {

        int[] integers = { 12, 13, 14, 17, 22, 27, 29, 32, 33, 35, 38, 41 };

        // here we use LINQ Query Syntax method to get query data

        var evenintegersQuery = from numeric in integers

                               where numeric % 2 == 0

                               select numeric;

        // here we use LINQ Method Syntax to extract array data

        var evenNumbersMethod = integers.Where(numeric => numeric % 2 == 0);

        // here is the Output results

        Console.WriteLine(“Display Even numeric with query syntax.”);

        foreach (var numeric in evenintegersQuery)

        {

            Console.WriteLine(numeric);  // Result is – 12 14 22 32 38

        }

        Console.WriteLine(“Display Even numeric with method syntax.”);

        foreach (var numeric in evenNumbersMethod)

        {

            Console.WriteLine(numeric);  // Result is – 12 14 22 32 38

        }

    }

}

Explain the LINQ connection method with the array data.

  • ऊपर यहाँ integers एक इंटीजर कन्टीन्यूस का एक ऐरे डाटा टाइप कलेक्शन है।
  • where numeric % 2 == 0 ऐरे कलेक्शन में से इवन नंबर को फिल्टर कर डिस्प्ले करता है।
  • यहाँ link कनेक्शन में sql डेटाबेस क्वेरी को क्वेरी सिंटैक्स और मेथड सिंटैक्स दोनों मेथड प्रोसेस का यूज़ करके डिस्प्ले किया गया है, और दोनों मेथड में आपको रिजल्ट एक जैसा ही मिलता है।

Common LINQ database connection query methods in C#.

Where – वेयर sql बेस्ड रिजर्व्ड कीवर्ड एक स्टेटमेंट कंडीशन के आधार पर डेटाबेस एलिमेंट डाटा वैल्यू कंटेंट को फिल्टर कर कंसोल स्क्रीन में डिस्प्ले करता है।

  • var evenIntegers = Integers.Where(n => n % 2 == 0);

Select – सलेक्ट भी sql बेस्ड रिजर्व्ड कीवर्ड एक डेटाबेस स्टेटमेंट क्वेरी कलेक्शन के हर डाटा एलिमेंट को एक नए फॉर्म में प्रोजेक्ट कर डिस्प्ले करता है।

  • var squareIntegers = Integers.Select(n => n * n);

OrderBy / OrderByDescending – यह sql बेस्ड रिजर्व्ड कीवर्ड डेटाबेस क्वेरी एलिमेंट को इनक्रीस या डिक्रीस आर्डर में सॉर्ट कर डिस्प्ले करता है।

  • var sortedIntegers = Integers.OrderBy(n => n);
  • var descendingIntegers = Integers.OrderByDescending(n => n);

GroupBy – यह sql बेस्ड डेटाबेस एलिमेंट्स डाटा को एक पर्टिकुलर की के अकॉर्डिंग ग्रुप कर डिस्प्ले करता है।

  • var groupedByParity = Integers.GroupBy(n => n % 2 == 0);

Aggregate – एक डेटाबेस ऐरे डाटा या कलेक्शन पर एक एक्युमुलेटर एग्रीगेट फ़ंक्शन अप्लाई करके एक सिंगल वैल्यू आउटपुट डिस्प्ले करता है।

  • var addtion = Integers.Aggregate((total, next) => total + next);

First / FirstOrDefault – यह डेटाबेस में पहला एलिमेंट या अगर कोई एलिमेंट नहीं मिलता है, तो डिफ़ॉल्ट वैल्यू आउटपुट को डिस्प्ले करता है।

  • var firstEvenintegers = integers.First(n => n % 2 == 0);
  • var firstEvenOrDefault = integers.FirstOrDefault(n => n % 2 == 0);

Distinct – यह ग्रुप ऑफ़ ऐरे डाटा टाइप कलेक्शन से डुप्लीकेट एलिमेंट डाटा वैल्यू को डिलीट करता है।

  • var distinctIntegers = Integers.Distinct();

Any / All – यह ग्रुप ऑफ़ ऐरे डाटा को चेक करता है कि कोई या सभी एलिमेंट कंडीशन को फुलफिल करते हैं, या नहीं है।

  • var anyEven = Integers.Any(n => n % 2 == 0); // यह एक्सप्रेशन में चेक करता है कि कोई इन्टिजर नंबर ईवन है, या नहीं है.
  • var allEven = Integers.All(n => n % 2 == 0); // यह एक्सप्रेशन में चेक करता है कि सभी इन्टिजर नंबर ईवन हैं, या नहीं है.

Example of Common LINQ database connection query.

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

{

    public static void Main()

    {

        List<int> Integers = new List<int>

        {

            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 6

        };

        // here we apply Where sql clause – Even numbers

        var evenIntegers = Integers.Where(n => n % 2 == 0);

        // here we use Select sql clause – Square of numbers

        var squareIntegers = Integers.Select(n => n * n);

        // herwe we use OrderBy sql clause – Ascending order data arrangement

        var sortedIntegers = Integers.OrderBy(n => n);

        // here we OrderByDescending sql clause – Descending order data order arrangement

        var descendingIntegers = Integers.OrderByDescending(n => n);

        // GroupBy – Group by parity sql clause (Even/Odd)

        var groupedByParity = Integers.GroupBy(n => n % 2 == 0);

        // here we Aggregate – Sum of all integer numbers

        var addition = Integers.Aggregate((total, next) => total + next);

        // here we use First – First even number

        var firstEvenIntegers = Integers.First(n => n % 2 == 0);

        // here we FirstOrDefault – First even number or default

        var firstEvenOrDefault = Integers.FirstOrDefault(n => n % 2 == 0);

        // here we use Distinct – Remove duplicates value

        var distinctIntegers = Integers.Distinct();

        // here we use Any – Check if any even numbers exist

        var anyEven = Integers.Any(n => n % 2 == 0);

        // here All – Check if all numbers are even

        var allEven = Integers.All(n => n % 2 == 0);

        // method to Display all link query output

        Console.WriteLine(“Original Integers:”);

        Console.WriteLine(string.Join(“, “, Integers));

        Console.WriteLine(“\n Even Integers:”);

        Console.WriteLine(string.Join(“, “, evenIntegers));

        Console.WriteLine(“\n Square Integers:”);

        Console.WriteLine(string.Join(“, “, squareIntegers));

        Console.WriteLine(“\n Sorted Integers (Ascending):”);

        Console.WriteLine(string.Join(“, “, sortedIntegers));

        Console.WriteLine(“\n Sorted Integers (Descending):”);

        Console.WriteLine(string.Join(“, “, descendingIntegers));

        Console.WriteLine(“\n Grouped By Parity:”);

        foreach (var group in groupedByParity)

        {

            string groupName = group.Key ? “Even” : “Odd”;

            Console.WriteLine($”{groupName}: {string.Join(“, “, group)}”);

        }

        Console.WriteLine($”\n Addition of Integers – {addition}”);

        Console.WriteLine($”\n First Even Integer – {firstEvenIntegers}”);

        Console.WriteLine($”First Even Or Default – {firstEvenOrDefault}”);

        Console.WriteLine(“\n Distinct Integers -“);

        Console.WriteLine(string.Join(“, “, distinctIntegers));

        Console.WriteLine($”\n Any Even Numbers – {anyEven}”);

        Console.WriteLine($”All Numbers Are Even – {allEven}”);

    }

}

LINQ to Objects SQL query example and methods.

यहाँ आपको एक और डिटेल्ड एक्साम्प्ल दिया गया है, जो आपको एक ही प्रोग्राम में कई LINQ मेथड को ग्रुप कर आउटपुट डिस्प्ले करता है.

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

{

    public static void Main()

    {

        var employees = new List<Employee>

        {

            new Employee { Emp_Name = “Bhavishi Deora”, Age = 24, Id = 101 },

            new Employee { Emp_Name = “Siddhi Deora”, Age = 22, Id = 102 },

            new Employee { Emp_Name = “Harry”, Age = 27, Id = 103 },

            new Employee { Emp_Name = “Amit”, Age = 23, Id = 104 },

            new Employee { Emp_Name = “Vivek”, Age = 37, Id = 105 }

        };

        // here we use sql Query syntax

        var query = from emp in employees

                    where emp.Age >= 21

                    select emp;

        // here we use sql Method syntax

        var method = employees.Where(emp => emp.Age >= 30);

        Console.WriteLine(“\n Display Employees info aged 21 or above with Query Syntax.”);

        foreach (var emp in query)

        {

            Console.WriteLine($”ID – {emp.Id}, Employee Name – {emp.Emp_Name}, Age – {emp.Age}”);

        }

        Console.WriteLine(“\n Display Employees age 21 or above with Method Syntax.”);

        foreach (var emp in method)

        {

            Console.WriteLine($”ID – {emp.Id}, Employee Name – {emp.Emp_Name}, Age – {emp.Age}”);

        }

        // here we use Group employees by Age

        var groupedByAge = employees.GroupBy(emp => emp.Age);

        Console.WriteLine(“\n Grouped by Age:”);

        foreach (var group in groupedByAge)

        {

            Console.WriteLine($”\n Age Group = {group.Key}”);

            foreach (var emp in group)

            {

                Console.WriteLine($”ID – {emp.Id}, Name – {emp.Emp_Name}”);

            }

        }

    }

    public class Employee

    {

        public string Emp_Name { get; set; }

        public int Age { get; set; }

        public int Id { get; set; }

    }

}

Objects SQL query example and methods explanation.

  • यहाँ इस एक्साम्प्ल में हमारे पास एक List<Employee> है, और हम Where, GroupBy, और Select जैसे sql क्लॉज़ LINQ मेथड को अप्लाई करते हैं। यहाँ स्टाइल में डिफ्रेंस प्रीव्यू करने के लिए डेटाबेस क्वेरी और मेथड सिंटैक्स साथ-साथ डिस्प्ले किए  गए हैं।

LINQ to SQL and Entity Framework in C#.

C# प्रोग्राम में LINQ कनेक्शन मेथड का यूज़ LINQ to SQL या Entity Framework के माध्यम से डेटाबेस के साथ भी किया जा सकता है। इसमें आप C# में डेटाबेस क्वेरी को क्रिएट कर सकते हैं. जिससे कि रिलेशनल डेटाबेस क्वेरी इनफार्मेशन के साथ बहुत ही रियल आर्डर में SQL जैसे स्टाइल से इंटरैक्ट या कम्यूनिकेट कर सकें।

Example of using the LINQ connection method with Entity Framework in C#.

using (var context = new MyDbContext())

{

var employees = context.Employees

.Where(e => e.Department == “MARKETING”)

.OrderBy(e => emp.Name)

.ToList();

}

Using the LINQ connection method with Entity Framework in the example.

  • यहाँ इस एक्साम्प्ल में context.Employees डेटाबेस में एक टेबल को डिस्प्ले करता है, और LINQ कनेक्शन डेटा को SQL की तरह ही क्वेरी करता है, लेकिन डायरेक्ट C# में।

Detailed summary of the LINQ database connection method.

  • C# यूजर प्रोग्राम में LINQ, Query Syntax या Method Syntax मेथड का यूज़ करके डायरेक्ट C# में छोटे, डिक्लेरेटिव आर्डर में डेटाबेस क्वेरी ऑपरेशन परफॉर्म करने में हेल्प करता है।
  • LINQ मेथड आपको sql डेटाबेस क्वेरी ऑपरेशन में Where, Select, GroupBy, और OrderBy जैसे डेटाबेस मेथड क्वेरी से डेटा मैनिपुलेशन प्रोसेस को इजी करता है।
  • LINQ C# यूजर को कई तरह के डेटा सोर्स एक्सेस रिट्रीवल, जैसे इन-मेमोरी कलेक्शन, डेटाबेस, XML, और भी बहुत कुछ क्वेरी रिट्रीव या एक्सेस करने में हेल्प करता है।
  • यहाँ डेफर्ड एग्जीक्यूशन एक मुख्य फीचर है, इसका मतलब है कि डेटाबेस क्वेरी तभी एग्जीक्यूट होती हैं, जब आपको आवश्यकता होती है। C# में LINQ एक कई तरह से यूज़  होने वाला इम्पोर्टेन्ट डेटाबेस एक्सेस क्वेरी टूल है. जिसे सामान्य रूप से कलेक्शन, डेटाबेस Entity Framework या LINQ to SQL, और यहां तक ​​कि XML डेटाबेस के साथ भी यूज़ किया जाता है।

Leave a Reply