Ci Ty Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: LinQ , C# , Projection Operators --- # Projection Operators - SelectMany SelectMany 的功用與 Select 相同 , 都是依照我們設定的委派去對資料源做出處理 , 並輸出. 但是差異是 Select 只取得第一層的查詢結果放到輸出序列中 , 但是 SelectMany 若發現查詢結果回傳的是一個可以列舉的序列 , 則會再進一步把這個序列中的項目取出來 , 放到輸出序列中 , 也就是用 SelectMany 運算子 , 查詢出來的輸出序列之深度會比 Select 少一層. SelectMany 通常是在處理巢狀集合的時候使用 , 可以替我們省去多層迴圈 . 一言以蔽之 , SelectMany 可以將集合中每個元素內的子集合**合併為一個新的集合**. 換句話說 , SelectMany 可以幫我們**把子集合展開成一個.** ### 使用時機 - 需要取出集合元素中的每一個子集合中的每一個元素. e.g. 學生物件有一個List<朋友> . 我們想要取出List<學生> 中所有的朋友物件. - 需要對集合元素中的每一個子集合中的每一個元素做投影的動作. 你喜歡跑雙層迴圈取值 , 那這個函數可以當作沒看到 :laughing: ### [多載形式](https://docs.microsoft.com/zh-tw/dotnet/api/system.linq.enumerable.selectmany?view=netframework-4.8) 1. SelectMany<TSource,TResult>(this IEnumerable<TSource>, Func<TSource,**IEnumerable<TResult>>**) * 將序列的每個項目都投影成 IEnumerable<T>,並將產生的序列簡化成單一序列。 2. SelectMany<TSource,TResult>(this IEnumerable<TSource>, Func<TSource,Int32,**IEnumerable<TResult>>**) * 將序列的每個項目都投影成 IEnumerable<T>,並將產生的序列簡化成單一序列。 各來源項目的索引是在該項目的投影表單中使用。 3. SelectMany<TSource,TCollection,TResult>(this IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) * 將序列的每個項目投影為 IEnumerable<T>、將產生的序列簡化成單一序列,並對其中的每個項目叫用結果選取器函式。 4. SelectMany<TSource,TCollection,TResult>(this IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) * 將序列的每個項目投影為 IEnumerable<T>、將產生的序列簡化成單一序列,並對其中的每個項目叫用結果選取器函式。 各來源項目的索引是在該項目的中繼投影表單中使用。 ##### 解釋 - 3 & 4 與 1 & 2 相比 - 多了一個 resultSelector 可以使用 , 它可以傳入TSource (原本集合的每一個元素)及 TCollection (子集合中的每一個元素) , 讓我們可以讓外層的資料跟內層的資料合為同一筆資料. 當然 , 因為有 TCollection 可以使用 , 自然也能再對 TCollection 做投影的處理. - 使用 1 以及 2 將需要展開的集合平展開成一個集合 - **index 是 source 的索引.** - Func **回傳 IEnumerable 型別**. 這代表你需要指定一個能走訪的集合回傳. ### 範例 有三個學生{小昂,小王,大名} - 小昂有一個朋友 , 兩個手機. - 小王有一個朋友 , 一個手機. - 大名有兩個朋友 , 一個手機. 試不使用雙層迴圈來取出他們的資訊並列出 1. 僅取出號碼. 2. 僅取出號碼 , 但同個人的號碼 , 需要用相同的編號. 3. 僅取出朋友姓名 , 但必須標註這是哪個學生的朋友. 4. 取出朋友的資訊 , 但必須與學生的資訊配對再一起. (湊數 , 我想不到好例子= =) #### 結構 ```C# public class Student { public string Name { get; set; } public List<Friend> Friends { get; set; } public List<string> Phones { get; set; } } public class Friend { public string Name { get; set; } } public static List<Student> GetStudents() { return new List<Student> { new Student{Name = "小昂", Friends=new List<Friend>{ new Friend {Name="V" } } ,Phones=new List<string>{ "0989782268","0920755420" }}, new Student{Name = "小王", Friends=new List<Friend>{ new Friend {Name="Q" } } ,Phones=new List<string>{ "1234567888" }}, new Student{Name = "大名", Friends=new List<Friend>{ new Friend {Name="T" }, new Friend { Name = "S" } } ,Phones=new List<string>{ "6666666666" }}, }; } ``` #### 測試程式 ```C# static void Main(string[] args) { var students = GetStudents(); var queryPhones = students.SelectMany((student) => student.Phones); foreach (var phone in queryPhones) { Console.WriteLine(phone); } Console.WriteLine(); var queryPhonesWithIndex = students.SelectMany((student, index) => student.Phones.Select(phone => $"編號 {index}. 的 {phone}")); ; foreach (var phone in queryPhonesWithIndex) { Console.WriteLine(phone); } Console.WriteLine(); var queryFriends = students.SelectMany(student => student.Friends, (student, friend) => $"{student.Name} 有朋友叫做 {friend.Name}"); foreach (var item in queryFriends) { Console.WriteLine(item); } Console.WriteLine(); var queryFriendsWithIndex = students.SelectMany((student, index) => student.Friends.Select(friend => new { ID = index, friend.Name }), (student, friend) => new { student, friend }); foreach (var item in queryFriendsWithIndex) { Console.WriteLine($"編號{item.friend.ID}號 的{item.student.Name} 有朋友叫做 {item.friend.Name}"); } Console.ReadKey(); } ``` #### 結果 ![6LsbQDz.png](https://github.com/s0920832252/LinQ-Note/blob/master/Resources/6LsbQDz.png?raw=true) --- ### 簡單實作自己的SelectMany 作法 - MySelectMany : 負責對參數做出檢查或是判斷 - MySelectManyIterator : 負責回傳 IEnumerable - 其實就是走訪指定的 Source - resultSelector : 將 subItem 轉換成結果. ```C# public static IEnumerable<TResult> MySelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector) { if (source is null || selector is null) throw new Exception("Exception"); return MySelectManyIterator(source, selector); } public static IEnumerable<TResult> MySelectManyIterator<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector) { int index = -1; foreach (var item in source) { checked { index++; } foreach (var subItem in selector(item, index)) { yield return subItem; } } } public static IEnumerable<TResult> MySelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) { if (source is null || collectionSelector is null || resultSelector is null) throw new Exception("Exception"); return MySelectManyIterator(source, collectionSelector, resultSelector); } private static IEnumerable<TResult> MySelectManyIterator<TSource, TCollection, TResult>(IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) { int index = -1; foreach (var item in source) { checked { index++; } foreach (var subItem in collectionSelector(item, index)) { yield return resultSelector(item, subItem); } } } ``` --- 參考 [Source Code](https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/SelectMany.cs) --- ### Thank you! You can find me on - [GitHub](https://github.com/s0920832252) - [Facebook](https://www.facebook.com/fourtune.chen) 若有謬誤 , 煩請告知 , 新手發帖請多包涵 # :100: :muscle: :tada: :sheep:

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully