← Back to C# Course | Chapter 9: Collections | Lesson 6 of 7

LINQ की Basics

LINQ आपको किसी collection के बारे में एक छोटे, readable तरीके से सवाल पूछने देता है, जैसे 'मुझे सिर्फ red वाले size के अनुसार sorted दिखाओ'। यह आपके लिए lists को filter, sort, और summarize करता है।

In this page:

  1. LINQ Basics
Syntax
csharp
using System.Linq;

var result = collection
    .Where(item => condition)
    .Select(item => expression)
    .ToList();

LINQ Basics

LINQ (Language Integrated Query) आपको Where, Select, और OrderBy जैसे methods का उपयोग करके collections को declaratively filter, transform, और aggregate करने देता है।

यह System.Linq का हिस्सा है और किसी भी IEnumerable<T> पर काम करता है। LINQ queries आमतौर पर एक readable statement में data pipelines express करने के लिए साथ chain की जाती हैं।

Note: LINQ methods lazily evaluated होते हैं — वे तब तक नहीं चलते जब तक आप result को enumerate न करें, जैसे foreach या .ToList() से।

उदाहरण: LINQ Basics

markup
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

        var evenSquares = numbers
            .Where(n => n % 2 == 0)
            .Select(n => n * n)
            .ToList();

        foreach (int n in evenSquares)
        {
            Console.WriteLine(n);
        }
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि कई LINQ queries lazily चलती हैं, इसलिए result सिर्फ तभी compute होता है जब आप इसे enumerate करें।
  2. using System.Linq; भूल जाना और एक missing method error पाना।
  3. किसी खाली sequence पर First() call करना, जो throw करता है; FirstOrDefault() का उपयोग करें।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.