Thuta Learning
C#
AdvancedProgrammingအခြေခံ

LINQ: Querying Collections

C#သင်ခန်းစာ 2825 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • LINQ: Querying Collections ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Normal, boundary နဲ့invalid cases ကိုကိုယ်တိုင်စမ်းနိုင်ရန်
  • Readable, type-safe နဲ့testable C# code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

LINQ: Querying Collections ကို ရိုးရိုးလေးနားလည်မယ်

LINQ သည် collection နှင့် အခြား data source များကို strongly typed query ရေးနိုင်စေသည်။ Where က filter လုပ်ပြီး Select က shape ပြောင်းပေးသည်။ Query တစ်ခုကို ToList() မခေါ်မချင်း deferred execution ဖြစ်နိုင်သည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Query ကိုloop အတွင်းအကြိမ်ကြိမ်enumerate လုပ်ခြင်း သို့database LINQ အားlocal LINQ လိုယူဆခြင်းကperformance ပြဿနာဖြစ်စေပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

LINQ ကstrongly typed sequences ကိုfilter, transform, order, group နဲ့aggregate လုပ်စေပါတယ်။ Query definition နဲ့execution ကိုခွဲသိပြီး deferred execution, multiple enumeration, `IEnumerable<T>` နဲ့remote `IQueryable<T>` translation ကွာခြားချက်ကိုစစ်ပါ။

Mental Model
  1. Task / collection
  2. .NET API + pipeline
  3. Typed result / failure

LINQ: Querying Collections ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

csharp
var scores = new[] { 68, 42, 91, 77, 55 };

var passed = scores
    .Where(score => score >= 60)
    .OrderByDescending(score => score)
    .Select(score => $"Score: {score}")
    .ToList();

passed.ForEach(Console.WriteLine);
Console.WriteLine($"Average: {scores.Average():0.0}");

LINQ ကstrongly typed sequences ကိုfilter, transform, order, group နဲ့aggregate လုပ်စေပါတယ်။ Query definition နဲ့execution ကိုခွဲသိပြီး deferred execution, multiple enumeration, `IEnumerable<T>` နဲ့remote `IQueryable<T>` translation ကွာခြားချက်ကိုစစ်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Task / collectionvar scores = new[] { 68, 42, 91, 77, 55 };

    LINQ သည် collection နှင့် အခြား data source များကို strongly typed query ရေးနိုင်စေသည်။ Where က filter လုပ်ပြီး Select က shape ပြောင်းပေးသည်။ Query တစ်ခုကို ToList() မခေါ်မချင်း deferred execution ဖြစ်နိုင်သည်။

  2. .NET API + pipeline.Select(score => $"Score: {score}")

    LINQ ကstrongly typed sequences ကိုfilter, transform, order, group နဲ့aggregate လုပ်စေပါတယ်။ Query definition နဲ့execution ကိုခွဲသိပြီး deferred execution, multiple enumeration, `IEnumerable<T>` နဲ့remote `IQueryable<T>` translation ကွာခြားချက်ကိုစစ်ပါ။

  3. Typed result / failureConsole.WriteLine($"Average: {scores.Average():0.0}");

    Products ကိုWhere/OrderBy/Select ဖြင့်DTO shape ပြောင်းပြီး source ပြောင်းမီ/ပြောင်းပြီးနောက် enumerate ကွာခြားချက်ကိုစမ်းပါ။

Execution Flow
  1. Task / collection
  2. .NET API + pipeline
  3. Typed result / failure

Success

LINQ: Querying Collections ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

Failure

Query ကိုloop အတွင်းအကြိမ်ကြိမ်enumerate လုပ်ခြင်း သို့database LINQ အားlocal LINQ လိုယူဆခြင်းကperformance ပြဿနာဖြစ်စေပါတယ်။

ရိုးရိုးလေး ရှင်းပြမယ်

LINQ သည် collection နှင့် အခြား data source များကို strongly typed query ရေးနိုင်စေသည်။ Where က filter လုပ်ပြီး Select က shape ပြောင်းပေးသည်။ Query တစ်ခုကို ToList() မခေါ်မချင်း deferred execution ဖြစ်နိုင်သည်။

csharp
var scores = new[] { 68, 42, 91, 77, 55 };

var passed = scores
    .Where(score => score >= 60)
    .OrderByDescending(score => score)
    .Select(score => $"Score: {score}")
    .ToList();

passed.ForEach(Console.WriteLine);
Console.WriteLine($"Average: {scores.Average():0.0}");
You should see
Score: 91
Score: 77
Score: 68
Average: 66.6

လက်တွေ့စမ်းကြည့်ရန်

Products collection ကို category ဖြင့် filter လုပ်ပြီး price အနိမ့်မှအမြင့် sort လုပ်ကာ name နှင့် price သာရွေးပါ။

Language Integrated Query (LINQ) — Microsoft Learn

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Products ကိုWhere/OrderBy/Select ဖြင့်DTO shape ပြောင်းပြီး source ပြောင်းမီ/ပြောင်းပြီးနောက် enumerate ကွာခြားချက်ကိုစမ်းပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Products ကိုWhere/OrderBy/Select ဖြင့်DTO shape ပြောင်းပြီး source ပြောင်းမီ/ပြောင်းပြီးနောက် enumerate ကွာခြားချက်ကိုစမ်းပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
csharp
var scores = new[] { 68, 42, 91, 77, 55 };

var passed = scores
    .Where(score => score >= 60)
    .OrderByDescending(score => score)
    .Select(score => $"Score: {score}")
    .ToList();

passed.ForEach(Console.WriteLine);
Console.WriteLine($"Average: {scores.Average():0.0}");

ရလဒ်: LINQ: Querying Collections ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Products ကိုWhere/OrderBy/Select ဖြင့်DTO shape ပြောင်းပြီး source ပြောင်းမီ/ပြောင်းပြီးနောက် enumerate ကွာခြားချက်ကိုစမ်းပါ။

Quick Quiz 1 / 3

LINQ: Querying Collections ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Task / collection → .NET API + pipeline → Typed result / failure
  • LINQ ကstrongly typed sequences ကိုfilter, transform, order, group နဲ့aggregate လုပ်စေပါတယ်။ Query definition နဲ့execution ကိုခွဲသိပြီး deferred execution, multiple enumeration, `IEnumerable<T>` နဲ့remote `IQueryable<T>` translation ကွာခြားချက်ကိုစစ်ပါ။
  • Query ကိုloop အတွင်းအကြိမ်ကြိမ်enumerate လုပ်ခြင်း သို့database LINQ အားlocal LINQ လိုယူဆခြင်းကperformance ပြဿနာဖြစ်စေပါတယ်။
  • Products ကိုWhere/OrderBy/Select ဖြင့်DTO shape ပြောင်းပြီး source ပြောင်းမီ/ပြောင်းပြီးနောက် enumerate ကွာခြားချက်ကိုစမ်းပါ။

Language Integrated Query (LINQ) — Microsoft Learn

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Query ကိုloop အတွင်းအကြိမ်ကြိမ်enumerate လုပ်ခြင်း သို့database LINQ အားlocal LINQ လိုယူဆခြင်းကperformance ပြဿနာဖြစ်စေပါတယ်။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Products ကိုWhere/OrderBy/Select ဖြင့်DTO shape ပြောင်းပြီး source ပြောင်းမီ/ပြောင်းပြီးနောက် enumerate ကွာခြားချက်ကိုစမ်းပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Score: 91 Score: 77 Score: 68 Average: 66.6

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

LINQ: Querying Collections | Thuta Learning