[{"data":1,"prerenderedAt":159},["ShallowReactive",2],{"note-\u002Fnotes\u002Fcsharp-linq-basics":3},{"id":4,"title":5,"body":6,"category":144,"date":145,"description":146,"draft":147,"extension":148,"meta":149,"navigation":150,"path":151,"seo":152,"sourcePath":153,"stem":154,"tags":155,"__hash__":158},"notes\u002Fnotes\u002Fcsharp-linq-basics.md","C# LINQ 查詢入門",{"type":7,"value":8,"toc":138},"minimark",[9,14,18,21,28,64,70,90,93,104,107,117,120,134],[10,11,13],"h2",{"id":12},"linq-在做什麼","LINQ 在做什麼",[15,16,17],"p",{},"LINQ 把「篩選、投影、排序」寫成可組合的查詢。同一套思維可用在記憶體集合與部分資料來源。",[10,19,20],{"id":20},"兩種寫法",[15,22,23,27],{},[24,25,26],"strong",{},"查詢語法","適合表達意圖：",[29,30,35],"pre",{"className":31,"code":32,"language":33,"meta":34,"style":34},"language-csharp shiki shiki-themes github-light github-dark","var result =\n    from item in items\n    where item.IsActive\n    select item.Name;\n","csharp","",[36,37,38,46,52,58],"code",{"__ignoreMap":34},[39,40,43],"span",{"class":41,"line":42},"line",1,[39,44,45],{},"var result =\n",[39,47,49],{"class":41,"line":48},2,[39,50,51],{},"    from item in items\n",[39,53,55],{"class":41,"line":54},3,[39,56,57],{},"    where item.IsActive\n",[39,59,61],{"class":41,"line":60},4,[39,62,63],{},"    select item.Name;\n",[15,65,66,69],{},[24,67,68],{},"方法語法","適合串接與重用：",[29,71,73],{"className":31,"code":72,"language":33,"meta":34,"style":34},"var result = items\n    .Where(item => item.IsActive)\n    .Select(item => item.Name);\n",[36,74,75,80,85],{"__ignoreMap":34},[39,76,77],{"class":41,"line":42},[39,78,79],{},"var result = items\n",[39,81,82],{"class":41,"line":48},[39,83,84],{},"    .Where(item => item.IsActive)\n",[39,86,87],{"class":41,"line":54},[39,88,89],{},"    .Select(item => item.Name);\n",[10,91,92],{"id":92},"延遲執行",[15,94,95,96,99,100,103],{},"多數 LINQ 運算子是延遲的：定義查詢時還不會跑，直到你列舉（",[36,97,98],{},"foreach","、",[36,101,102],{},"ToList","）才執行。",[15,105,106],{},"這代表：",[108,109,110,114],"ul",{},[111,112,113],"li",{},"可以先組查詢再決定要不要物化",[111,115,116],{},"也要小心在迴圈裡重複列舉造成重複成本",[10,118,119],{"id":119},"小結",[15,121,122,123,126,127,130,131,133],{},"先用 ",[36,124,125],{},"Where"," \u002F ",[36,128,129],{},"Select"," 把資料收成你要的形狀，需要時再 ",[36,132,102],{},"。查詢語法與方法語法可以並存，選可讀性較高的那一種。",[135,136,137],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":34,"searchDepth":48,"depth":48,"links":139},[140,141,142,143],{"id":12,"depth":48,"text":13},{"id":20,"depth":48,"text":20},{"id":92,"depth":48,"text":92},{"id":119,"depth":48,"text":119},"backend","2026-07-18","用查詢語法與方法語法對照，釐清 Where、Select 與延遲執行的基本用法。",false,"md",{},true,"\u002Fnotes\u002Fcsharp-linq-basics",{"title":5,"description":146},null,"notes\u002Fcsharp-linq-basics",[156,157],"C#","LINQ","r3Gm7My6UeB0XayprWlDG4UUv4QeB2RNr51xOJlpoJI",1788767747620]