Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
UnityでのLINQ活用例
Search
RyotaMurohoshi
February 20, 2015
Technology
0
17
UnityでのLINQ活用例
2015/02/20(金)の勉強会の資料です。
※元々はSlideShareで公開していた資料です。
RyotaMurohoshi
February 20, 2015
Tweet
Share
More Decks by RyotaMurohoshi
See All by RyotaMurohoshi
Unityの合同同人誌や合同商業誌を書いてる僕は感想やレビューや評価が欲しい
ryotamurohoshi
0
370
Unity 2021.1での Unityパッケージの名称変更について
ryotamurohoshi
0
630
Odin Validationはいいぞ!
ryotamurohoshi
2
860
Tilemapはいいぞ!2020 〜すごいぞ、プロジェクト専用拡張Brush〜
ryotamurohoshi
0
2.3k
Unityでも、新しいC#
ryotamurohoshi
0
1.2k
Riderはいいぞ!
ryotamurohoshi
1
3.2k
Riderのススメ〜俺はRiderここが好き〜
ryotamurohoshi
1
2.2k
Unity開発者に伝えたい.NETのこと
ryotamurohoshi
4
36k
ImportedLinqのススメ
ryotamurohoshi
0
1.3k
Other Decks in Technology
See All in Technology
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
Taming you application's environments
salaboy
0
190
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
13k
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
OS 標準のデザインシステムを超えて - より柔軟な Flutter テーマ管理 | FlutterKaigi 2024
ronnnnn
0
150
The Role of Developer Relations in AI Product Success.
giftojabu1
0
130
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
9
1k
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
130
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
490
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Optimizing for Happiness
mojombo
376
70k
Facilitating Awesome Meetings
lara
50
6.1k
Being A Developer After 40
akosma
87
590k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Teambox: Starting and Learning
jrom
133
8.8k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Ruby is Unlike a Banana
tanoku
97
11k
Become a Pro
speakerdeck
PRO
25
5k
A Philosophy of Restraint
colly
203
16k
Transcript
UnityͰͷLINQ׆༻ྫ ྄ࣨଠ 2015/02/20(ۚ)
࣭Ͱ͢
UnityͰήʔϜΛ࡞͍ͯ͠Δํʁ
LINQ͍ͬͯΔํʁ
LINQ͍ͬͯΔํʁ
LINQ͕ͬͭΓ͍ͬͯΔํʁ
͋Γ͕ͱ͏͍͟͝·ͨ͠
UnityͰͷLINQͷ׆༻ࣄྫΛ௨͠ɺ LINQͷྑ͞ར༻ํ๏Λհ͠·͢ɻ
ࠓͷඪ Έͳ͞Μʹ • LINQΛ͏ͱίʔυ͕ಡΈ͘͢ͳΔ͜ͱΛͬͯΒ͏ • LINQΛ׆༻Ͱ͖Δγʔϯ͕ଟ͍͜ͱΛͬͯΒ͏ • LINQΛͬͯΈΑ͏ɾษڧͯ͠ΈΑ͏ͱࢥͬͯΒ͏ • LINQʹ͜Μͳศརͳϝιου͋ͬͨΜͩͱͬͯΒ͏
UnityͰͷྫΛ௨ͯ͠ʂ
ͱ͜ΖͰ
ήʔϜ։ൃͰେͳͷ ͨ͘͞Μ͋Γ·͢ΑͶ
ͦͷதͷͻͱͭ
؆ܿͰಡΈ͍͢ίʔυ
ͳ͔ͥͬͯʁ ಈ͚ಉ͡͡Όͳ͍͔ͬͯʁ
༷ɺมΘΓ·͢ΑͶʁ ϦϦʔεޙΞϓσ͠·͢ΑͶʁ
ʮԚ͍ίʔυͰͱΓ͋͑ͣಈ͘ʯ ظతʹૣ͍͔͠Εͳ͍ ×"ظతͳ։ൃ ̋!தɾظతͳ։ൃ
༷։ൃ్தͰมΘΓ·͢ΑͶʁ ؆ܿͰಡΈ͘͢حྷͳίʔυ ͜ΕʹൺͯԚ͍͘ίʔυͷ ༷มߋͱͯେมʂ
༡ͼͳ͕Βࢼߦࡨޡ͠໘ന͍Λ࡞Δ ͦΕʹ؆ܿͰಡΈ͍҆ίʔυ͕ඞཁʂ ͋ͱԚ͍͘ίʔυɺ·͡Ξϓσ͠ΜͲ͍ʂ
ಡΈ͍͢ίʔυେ ࢍ͍͚ͯͨͩ͠·͔͢ʁ
ͱ͜ΖͰࠓͷςʔϚ LINQͰ͢
LINQΛ͏ͱɺ ϦετͳͲͷίϨΫγϣϯΛѻ͏ίʔυ͕ɺ ؆ܿʹಡΈ͘͢ͳΓ·͢Αʂ
ͯ͞ɺLINQͱ
LINQͱ ౷߹ݴޠΫΤϦ!(LINQ)!ɺڧྗͳΫΤϦػೳΛ!C#!ݴޠ͓Αͼ! Visual!Basic!ݴޠͷߏจͱ֦ு͢ΔɺVisual!Studio!2008!Ͱಋೖ ͞ΕͨػೳͷηοτͰ͢ɻ! MSDN%LINQΑΓh*ps:/ /msdn.microso7.com/ja:jp/library/bb397926.aspx
ΠϝʔδΘ͖·͢ʁ
ྻͱ͔Ϧετͱ͔σΟΫγϣφϦͱ͔ γʔέϯε(↑ͷΑ͏ͳΦϒδΣͷू·Γ)Λ Λѻ͏ίʔυ͕LINQΛ͏ͱ៉ྷʹॻ͚Δ ͬͯ͜ͱ͚ͩͱΓ͋͑ͣࠓΠϝʔδ͍ͯͩ͘͠͞(దͰ͍͢·ͤΜ) ࠓ͓͢͠ΔͷɺLINQ%to%ObjectsͰ͢ ࠓͷͰʮLINQ͕͑ΔΑ͏ʹͳΔʯͱͳΓ·ͤΜɻΈͳ͞Μʹศར͞Λհ͠Α͏ͱࢥ͍·͢ɻ
ࣗݾհ • ໊લ:྄ࣨଠ • ॴଐ:Fuller,)Inc. • ࣄ:UnityΛ༻͍ͨήʔϜ։ൃ • ߘઌ:h3p:/ /qiita.com/RyotaMurohoshi
• Twi3er:@RyotaMurohoshi • ίϛϡχςΟ:AndroidͷձUnity෦
ϥΠϒϥϦUniLinq࡞ͬͨΓ UniBookͰLINQʹ͍ͭͯॻ͍ͨΓ
LINQେ͖Ͱ͢ʂ ʮLINQͷͳ͍C#ͳΜͯC#͡Όͳ͍ʯ ͬͯݴ͏ํ͍·͕͢ɺࢲͦ͏ࢥ͍·͢ʂ
ͦΜͳࢲ͕ ͜͏͍͏෩ʹ͑Δɺ͜Μͳঢ়گͰ͑Δ ͱ͍͏ྫΛΨϯΨϯհ͍͖ͯ͠·͢
ୈҰ෦ ࠓ͙͢ʹͬͯཉ͍͠ϝιου3બ CountɾAllɾAny
͜ΜͳΫϥε͕͋ͬͯ public class Monster { public int Id { get;
set; } public string Name { get; set; } public int Hp { get; set; } /* தུ */ }
List<Monster>ͷதͰHp͕0ҎԼͷཁૉΛ͑Δ ͷΑ͏ͳϦετͷࢦఆͷ݅Λຬͨͨ͠ཁૉΛ͑Δ ͬͯ͜ͱΓ·ͤΜʁ
Count ಛఆͷ݅Λຬͨͨ͠ཁૉΛ͑Δ
Hp͕0ҎԼͷཁૉɺͭ·ΓࢮΜͰ͍Δ Ϟϯελʔ(Monster)ͷΛ͍͑ͨ
Α͋͘Δforeachɺifͷίʔυ List<Monster> monsterList = LoadMonsterList (); // LINQΛΘͳ͍ͱ int deadMonsterCount
= 0; foreach (Monster monster in monsterList) { if(monster.Hp <= 0) { deadMonsterCount++; } }
Count ࢦఆͷ݅Λຬͨͨ͠ཁૉΛ͑Δ List<Monster> monsterList = LoadMonsterList (); // Hp͕0ҎԼͷཁૉͷʹ int
deadMonsterCount = monsterList.Count ((Monster monster) => monster.Hp <= 0);
͖ͬ͞ͷ2ͭͷίʔυ Ͳ͏ಡΊ·͢ʁ
͜ͷίʔυɺͲ͏ಡΈ·͢ʁ List<Monster> monsterList = LoadMonsterList (); // LINQΛΘͳ͍ͱ int deadMonsterCount
= 0; foreach (Monster monster in monsterList) { if(monster.Hp <= 0) { deadMonsterCount++; } }
intܕͷdeadMonsterCountΛ0ͰॳظԽ foreachจͰmonsterListΛ·Θ͢ ͠ཁૉͷmonsterͷHp͕0ҎԼͳΒ deadMonsterCountΛΠϯΫϦϝϯτ
͋ɺͭ·Γʂ HP͕0ҎԼͷϞϯελʔΛ͑Δͷ͔ʂ ͱ͍͏1ճ಄ͷதͰͷม͕ඞཁ
ҰํLINQʁͲ͏ಡΈ·͢ʁ // LINQΛ͏ͱ List<Monster> monsterList = LoadMonsterList (); // Hp͕0ҎԼͷཁૉͷʹ
int deadMonsterCount = monsterList.Count ((Monster monster) => monster.Hp <= 0);
intܕͷdeadMonsterCountΛ࣍ͷͰॳظԽ monsterListͷ࣍ͷ݅Λຬͨ͢ཁૉͩʂ ݅ཁૉͷHP͕0ҎԼͩʂ
΄΅ҙຯతʹɺ ʮHp͕0ҎԼͷϞϯελʔΛ͑Ζʂʯ ࠨ͔ΒӈʹૉʹಡΊΔʂ
foreachจɺifจͷ ʮͲ͏ॲཧ͍ͯ͠Δ͔ʯ͕ॻ͔Ε͍ͯΔ LINQͰ ʮԿ͕͍͔ͨ͠ʯ͕ॻ͔Ε͍ͯΔ
͔ͩΒLINQͷίʔυಡΈ͍͢ ͔͠؆ܿ
͔͜͜ΒجຊɺLINQ൛ͷΈͰ͘͘͞͞ߦ͖·͢
Any ಛఆͷ݅Λຬͨ͢ཁૉ͕ ଘࡏ͢Δ͔ௐΔ
Any ಛఆͷ݅Λຬͨ͢ཁૉ͕ଘࡏ͢Δ͔ௐΔ List<Monster> monsterList = LoadMonsterList (); // Hp͕0ҎԼͷϞϯελʔ͕ଘࡏ͢Εtrue bool
isExistDeadMonster = monsterList.Any((Monster monster) => monster.Hp <= 0);
All શͯͷཁૉ͕ࢦఆͷ݅Λ ຬ͔ͨ͢ௐΔ
All શͯͷཁૉ͕ࢦఆͷ݅Λຬ͔ͨ͢ௐΔ List<Monster> monsterList = LoadMonsterList (); // શͯͷϞϯελʔͷHp͕0ҎԼͳΒtrue bool
isAllMonsterDead = monsterList.All((Monster monster) => monster.Hp <= 0);
࣍UnityͬΆ͍ͷͰ ͔ͭ ׂͱϚΠφʔͳͭΛ
ୈೋ෦ Editor֦ுͰ׆༂ OfType
OfType ࢦఆͷܕͷཁૉͷΈʹϑΟϧλϦϯά͠ɺ ࢦఆͷܕͷγʔέϯεʹม
ProjectΟϯυதͰબதͷͷதͰɺ ςΫενϟ(TextureΫϥε)ͷΈΛॲཧ͢Δ (UnityEditor֦ு)
• UnityEditor.Selec0onΫϥεͷobjectsϓϩύςΟΛར༻ • Selec0on.objectsݱࡏબ͍ͯ͠ΔͷΛऔಘ • ܕUnityEngine.Object[]Ͱऔಘ • ͜ΕʹવςΫενϟ(TextureΫϥε)Ҏ֎ͷͷؚ·ΕΔ • ͚ͲTextureͷΈΛରʹ͍ͨ͠
ϑΟϧλϦϯάͱ͍͑Where ͜ΕΛisԋࢉࢠͱ͋Θͤͯ foreach (UnityEngine.Object target in Selection.objects.Where (o => o
is Texture)){ // Ωϟετ͢Δ or asԋࢉࢠར༻͕ΊΜͲ͍ Texture texture = target as Texture; // ͜͜ͰTextureʹԿ͔͠Βͷॲཧ }
CastϝιουͰΩϟετͰ͖·͕͢ foreach (Texture texture in Selection.objects .Where (o => o
is Texture) .Cast<Texture>()){ // ͜͜ͰTextureʹԿ͔͠Βͷॲཧ }
ͦ͜ͰOfTypeͰ͢Αʂ
OfType ࢦఆͷܕͷཁૉͷΈʹϑΟϧλϦϯά͠ɺࢦఆͷܕͷγʔέϯεʹม // Selection.objectsͷதͷཁૉΛTextureܕͷΈʹϑΟϧλϦϯά͠ɺ // IEnumerable<Texture>ʹม foreach (Texture texture in
Selection.objects.OfType<Texture> ()){ // ͜͜ͰTextureʹԿ͔͠Βͷॲཧ }
UnityͷEditor֦ுͰɺSelec.onར༻γʔϯ͕ଟ͍ʂ (MenuItemͱ߹Θͤͯಋೖָ͕ʂ) ʮSelec%on.objects.OfType<Texture>7()ʯɺͥͻʂ ࣮ࡍʹͬͨྫ͕ͪ͜Βʂ!h#p:/ /qiita.com/RyotaMurohoshi/items/b01e3cdb91fea96f4574
OfTypeϝιου ଞͰNUnitLiteͱ͍͏ϥΠϒϥϦΛͬͨࡍ System.Object͔ΒಛఆͷܕͷมͰ͍·ͨ͠
Editor֦ுͰͷLINQ׆༻ LINQɺ࣮iOSͰ͕͋Γ·͢ ͚ΕͲUnityEditorͰ͋Εେৎʂ Editor֦ு͔Βར༻࢝͠ΊΔͷΦεεϝʂ
ୈࡾ෦ ࢦఆͷ݅Λຬͨ͢ઌ಄ͷཁૉΛऔಘ͢Δ FirstɺFirstOrDefault
List<Monster>ͷதͰHp͕0ҎԼͷཁૉ͕Ұͭཉ͍͠ ͷΑ͏ͳϦετͷࢦఆͷ݅Λຬͨ͢ཁૉΛҰͭऔಘ͢Δ ͬͯ͜ͱΓ·ͤΜʁ
First ࢦఆͷ݅Λຬͨ͢ઌ಄ͷཁૉΛऔಘ͢Δ ݅Λຬͨͨ͠ཁૉ͕ͳ͍߹ྫ֎ൃੜ
First ࢦఆͷ݅Λຬͨ͢ઌ಄ͷཁૉΛऔಘ͢Δɻ݅Λຬͨͨ͠ཁૉ͕ͳ͍߹ྫ֎ൃੜ List<Monster> monsterList = LoadMonsterList (); // ͻͱͭHp͕0ҎԼͷཁૉ͕ଘࡏ͠ͳ͍ͳΒྫ֎ൃੜ Monster
deadMonster = monsterList .First ((Monster monster) => monster.Hp <= 0);
Firstϝιου ݅Λຬͨ͢ཁૉ͕ͳ͍߹ྫ֎͕ൃੜʂ ͡Ό͋ྫ֎Λൃੜͨ͘ͳ͍߹ʁ AnyͰଘࡏνΣοΫͯ͋ͬͨ͠߹ͷΈɺ͘͠ɽɽɽ
FirstOrDefault ࢦఆͷ݅Λຬͨ͢ઌ಄ͷཁૉΛऔಘ͢Δ ݅Λຬͨͨ͠ཁૉ͕ͳ͍߹ ͦͷܕͷنఆΛฦ͢
݅Λຬͨͨ͠ཁૉ͕ͳ͍߹ɺ ʮنఆΛฦ͢=nullΛฦ͢ʯ͡Όͳ͍ʂ ͜͜ཁҙʂܕͷنఆnull͡Όͳ͍ʂ
نఆ • Ϋϥεܕͷ߹ɺnull • intͷ߹ɺ0 • floatͷ߹ɺ0.0F • boolͷ߹ɺfalse •
Vector3ͷ߹ɺxɺyɺz͕શͯ0.0F
FirstOrDefault ࢦఆͷ݅Λຬͨ͢ઌ಄ͷཁૉΛऔಘ͢Δɻ݅Λຬͨͨ͠ཁૉ͕ͳ͍߹ͦͷܕͷنఆΛฦ͢ List<Monster> monsterList = LoadMonsterList (); // ͻͱͭHp͕0ҎԼͷཁૉ͕ଘࡏ͠ͳ͍ͳΒMonsterΫϥεͷنఆnullʹ Monster
deadMonster = monsterList .FirstOrDefault ((Monster monster) => monster.Hp <= 0);
͜͜Ͱ͞Βʹ͏Ұͭʂ
DefaultIfEmpty γʔέϯε͕ۭͳΒҾͰͨ͠ͷΛ །Ұͷཁૉͱ͢Δγʔέϯεʹม
FirstͱΈ߹Θͤͯ͜Μͳ͜ͱ͕Ͱ͖Δ
DefaultIfEmpty γʔέϯε͕ۭͳΒҾͰͨ͠ͷΛ།Ұͷཁૉͱ͢Δγʔέϯεʹม List<Monster> monsterList = LoadMonsterList (); Monster defaultMonster =
GetDefaultMonster (); int targetId = GetTargetId (); // ࢦఆIDͷϞϯελʔΛ୳͢ɻͳ͔ͬͨΒdefaultMonsterʹɻ Monster targetMonster = monsterList .Where ((Monster monster) => monster.Id == targetId) .DefaultIfEmpty (defaultMonster) .First (); // ͜ͷΦʔόʔϩʔυ୯७ʹઌ಄ཁૉऔಘ
ͱ͜ΖͰ
σϦήʔτͱϥϜμࣜ • ʮ(Monster*monster)*=>*monster.Hp*<=*0ʯϥϜμࣜ • ʮϥϜμࣜʯͰͳ͘ʮσϦήʔτʯΛҾʹͱΔ • LINQͷଟ͘ͷϝιου͕Func<T,*TResult>ΛऔΔ • ϥϜμࣜͷ༻్ͷҰͭσϦήʔτͷੜ ৄ͘͠ʂɹh"p:/
/qiita.com/RyotaMurohoshi/items/740151bd772889cf07deɹʹ·ͱΊ·ͨ͠
ୈ࢛෦ ࢉज़ܭࢉܥ MaxɺMinɺSumɺAverage
͜ΜͳΫϥεΛ͍·͢ public class PlayLog { public int StageId { get;
set; } public int Score { get; set; } public DateTime PlayedAt { get; set; } public TimeSpan PlayTime { get; set; } }
List<PlayLog>ͷཁૉͷதͰҰ൪େ͖͍ScoreΛٻΊΔ ͷΑ͏ͳϦετཁૉΛมͯ͠ɺͦͷҰ൪େ͖͍ΛٻΊΔ ͬͯ͜ͱΓ·ͤΜʁ
Max ֤ཁૉʹมΛࢪͦ͠ͷ࠷େΛܭࢉ͢Δ
Max ֤ཁૉʹมΛࢪͦ͠ͷ࠷େΛܭࢉ͢Δ List<PlayLog> playLogList = LoadPlayLogList (); // PlayLogListͷScoreͷ࠷େ int
maxScore = playLogList.Max ((PlayLog playLog) => playLog.Score);
Min ֤ཁૉʹมΛࢪͦ͠ͷ࠷খΛܭࢉ͢Δ
Min ֤ཁૉʹมΛࢪͦ͠ͷ࠷খΛܭࢉ͢Δ List<PlayLog> playLogList = LoadPlayLogList (); // PlayLogListͷScoreͷ࠷খ int
minScore = playLogList.Min ((PlayLog playLog) => playLog.Score);
Sum ֤ཁૉʹมΛࢪͦ͠ͷ߹ܭΛܭࢉ͢Δ
Sum ֤ཁૉʹมΛࢪͦ͠ͷ߹ܭΛܭࢉ͢Δ List<PlayLog> playLogList = LoadPlayLogList (); // PlayLogListͷScoreͷ߹ܭ int
sumOfScore = playLogList.Sum ((PlayLog playLog) => playLog.Score);
Average ֤ཁૉʹมΛࢪͦ͠ͷฏۉΛܭࢉ͢Δ
Average ֤ཁૉʹมΛࢪͦ͠ͷฏۉΛܭࢉ͢Δ List<PlayLog> playLogList = LoadPlayLogList (); // PlayLogListͷScoreͷฏۉ double
averageOfScore = playLogList.Average ((PlayLog playLog) => playLog.Score);
ͱ͜ΖͰ
LINQͱiOS(1) • Unity+iOSͩͱ࣮ػ࣮ߦ࣌ΤϥʔʹͳΔ͜ͱ͕͋Δ • ཧ༝AOTίϯύΠϧ͕͏·͍͍ͬͯ͘ͳ͍Έ͍ͨ • ͦΕΛղܾ͢ΔͨΊʹUniLinq͍ͬͯ͏ͭΛ࡞ͬͨ
LINQͱiOS(2) • IL2CPPొ • IL2CPPͰେৎ͔ࢼͨ͠Βɺςετ͢Δ͕࣮ͭػͰΤϥʔʹ • AssemblyΫϥεͷCodePathϓϩύςΟ͕nullͳͷ͕ݪҼ • ࠓޙਖ਼͘͠ಈ͔͘Ͳ͏͔ͳͲɺܧଓͯ͠ௐ͍͖ͯ·͢ʂ
ୈޒ෦ ฒͼସ͑ OrderByɺOrderByDescending ThenByɺThenByDescending
List<PlayLog>ΛScroreͷ߱ॱͰฒͼม͑Δ ͷΑ͏ͳྻૢ࡞ ͬͯΓ·ͤΜʁ
OrderBy ֤ཁૉʹมΛࢪ͠ ͦͷ݁ՌͰฒͼସ͑
OrderBy ֤ཁૉʹมΛࢪͦ͠ͷ݁ՌͰฒͼସ͑ List<PlayLog> playLogList = LoadPlayLogList (); // είΞͷ߱ॱͰฒͼସ͑ IEnumerable<PlayLog>
orderedPlayLog = playLogList .OrderByDescending ((PlayLog playLog) => playLog.Score); OrderByঢॱͰฒͼସ͑ɺOrderByDescending߱ॱͰฒͼସ͑ ݩͷσʔλΛฒͼม͑ΔͷͰͳ͘ɺ৽ͨʹฒͼସ͑ͨγʔέϯεΛͭ͘Δ
͠ಉ͡ScoreͳΒͲ͏͠Α͏ʁ ಉScoreͳΒPlayTime͕͍ॱʹ͍ͨ͠߹ʁ
͏ҰOrderByΛݺͿͷؒҧ͍ʂ
ThenBy OrderByͰฒͼସ͑ͨͷͰɺ ಉධՁͩͬͨͷΛฒͼସ͑
ThenBy OrderByͰฒͼସ͑ͨͷͰɺಉධՁͩͬͨͷΛฒͼସ͑ List<PlayLog> playLogList = LoadPlayLogList (); // είΞͷ߱ॱͰฒͼସ͑ͯɺಉͳΒ͕͍࣌ؒํΛઌʹ IEnumerable<PlayLog>
orderedPlayLog =ɹplayLogList .OrderByDescending ((PlayLog playLog) => playLog.Score) .ThenBy ((PlayLog playLog) => playLog.PlayTime);
ୈ෦ εΩοϓͱऔಘ SkipɺTakeɺSkipWhileɺTakeWhile
σʔλΛը໘ʹ10ݸͣͭදࣔ ΠϯσοΫε0ͷ1ϖʔδɺ0൪͔Β9൪ ΠϯσοΫε1ͷ2ϖʔδɺ10൪͔Β19൪ ΠϯσοΫε2ͷ3ϖʔδɺ20൪͔Β29൪ ͬͯ͜ͱΓ·ͤΜ
Skip%+%Take
Skip ઌ಄͔ΒҾͰͨ͠ݸ͚ͩ ཁૉΛඈ͢
Take ઌ಄͔ΒҾͰͨ͠ݸ͚ͩ ཁૉΛฦ͢
Skip+Take σʔλΛը໘ʹ10ݸͣͭදࣔɺࠓճ3ϖʔδ(ΠϯσοΫε2) List<PlayLog> playLogList = LoadPlayLogList (); int numPerPage =
10; int pageIndex = 2; int skipNum = numPerPage * pageIndex; // 3ϖʔδ(ΠϯσοΫε2)ɺ20൪͔Β10ݸͷཁૉΛऔಘ IEnumerable<PlayLog> selectedPlayLogs =ɹplayLogList .Skip (skipNum) .Take (numPerPage);
ࠓɺDateTimeܕͷPlayedAtͷঢॱͰPlayLog͕ฒΜͰ͍Δ ͋Δ࣌ࠁ͔Βɺଞͷ͋Δ࣌ࠁ·ͰͷσʔλΛऔಘ͍ͨ͠
SkipWhile)+)TakeWhile
SkipWhile ઌ಄͔ΒҾͰ͕ͨ݅͠ਅͷؒ ཁૉΛඈ͢
TakeWhile ઌ಄͔ΒҾͰ͕ͨ݅͠ਅͷؒ ཁૉΛฦ͢
SkipWhile)+)TakeWhile List<PlayLog> playLogList = LoadPlayLogList (); DateTime start = GetStartTime
(); DateTime end = GetEndTime (); // PlayedAt͕startΑΓޙorಉ࣌ࠁͰɺendΑΓલͳཁૉΛऔಘ IEnumerable<PlayLog> selectedPlayLogs =ɹplayLogList .SkipWhile ((PlayLog playLog) => playLog.PlayedAt < start) .TakeWhile ((PlayLog playLog) => playLog.PlayedAt < end);
ୈࣣ෦ ͦͷଞΦεεϝ
ElementAt γʔέϯεதͷ ࢦఆͷΠϯσοΫεͷཁૉΛऔಘ
⚪൪ΠϯσοΫεͷཁૉ͕ཉ͍͠ ͜͏͍͏࣌ɺ͋Γ·͢ΑͶɻͲ͏͠·͢ʁ ͋·Γॻ͔ͳ͍͔͠Εͳ͍͚ͲɺϦϑΝΫλϦϯάͷ్தͰඞཁͳ͜ͱ͋Δʁ
ଟ͘ͷLINQϝιουͷฦܕͰ͋ΔɺIEnumerable<T>ܕ ΠϯσΫαͰཁૉʹΞΫηεͰ͖ͳ͍ʂ
ToArrayμϝઈରʂ // ToArrayͰྻʹ͔ͯ͠ΒΞΫηεʁ // ޮѱ͍ʂʂʂ // playLogsIEnumerable<PlayLog>ܕ PlayLog index5PlayLog =
playLogs.ToArray ()[5];
ElementAt // ΠϯσΫαͳ͍͚ΕͲ // ElementAtϝιουͰΠϯσοΫεʹΞΫηεͰ͖Δ PlayLog index5PlayLog = playLogs.ElementAt (5);
ToDic&onary γʔέϯε͔ΒࣙॻΛ࡞Δ ΩʔΛࢦఆ͢ΔɻόϦϡʔࢦఆͰ͖Δɻ
͜ΜͳΫϥεΛ͍·͢ public class Item { public int Id { get;
set; } public string Name { get; set; } public ItemType ItemType { get; set; } } public enum ItemType { Weapon, ConsumptionItem, ValuableItem }
List<Item>*͔ΒItemͷIdΛΩʔͱ͢Δ Dic$onary<int,-Item>Λͭ͘Δ Έ͍ͨͳ͜ͱͨ͋͘͠Γ·ͤΜʁ ͦΕLINQͰҰߦͰ͔͚·͢ʂ
ToDic&onary γʔέϯε͔ΒࣙॻΛ࡞Δ!(ͦͷ1) List<Item> itemList = LoadItemList (); // IdϓϩύςΟΛΩʔͱ͢ΔσΟΫγϣφϦͷੜ //
͠Ωʔͷॏෳ͕͋ͬͨΒྫ֎͕ൃੜ Dictionary<int, Item> itemDict = itemList .ToDictionary((Item item) => item.Id);
ToDic&onary γʔέϯε͔ΒࣙॻΛ࡞Δ!(ͦͷ2) List<Item> itemList = LoadItemList (); // IdϓϩύςΟΛΩʔɺNameϓϩύςΟόϦϡʔͱ͢Δ //
σΟΫγϣφϦͷੜ // ͠Ωʔͷॏෳ͕͋ͬͨΒྫ֎͕ൃੜ Dictionary<int, string> itemDict = itemList.ToDictionary( (Item item) => item.Id, (Item item) => item.Name);
ToLookup γʔέϯε͔ΒILookupΛ࡞Δ άϧʔϓԽͨ͠ίϨΫγϣϯΛੜ
List<Item>*itemList͕͋ͬͯ Dic$onary<ItemType,2List<Item>>͕ཉ͍͠ LINQʹDic(onary<ItemType,5List<Item>>ʹࣅ͍ͯΔ ILookup<ItemType,.Item>͕࡞ΕΔϝιου͕͋Δ
ToLookup γʔέϯε͔ΒILookupΛ࡞Δ(άϧʔϓԽͨ͠ίϨΫγϣϯΛੜ) List<Item> itemList = LoadItemList (); // ILookup<ItemType, Items>
// Dictionary<ItemType,List<Item>>ʹࣅ͍ͯΔ ILookup<ItemType, Item> itemLookup = itemList. ToLookup((Item item) => item.ItemType); // σΟΫγϣφϦͷΑ͏ʹ[]ͱΩʔͰͦͷάϧʔϓʹΞΫηεͰ͖Δ IEnumerable<Item> weapons = itemLookup[ItemType.Weapon];
SelectMany ϦετͷϦετͷฏԽΛߦ͏
SelectMany ϦετͷϦετͷฏԽΛߦ͏(ͦͷ1) // ϞϯελʔͷϦετͷϦετ List<List<Monster>> listOfMonsterList = LoadListOfMonsterList (); //
Ϟϯελʔͷγʔέϯεʹ IEnumerable<Monster> monsters = listOfMonsterList .SelectMany ((List<Monster> list) => list);
SelectManyϦετͷϦετ͚͕ͩର͡Όͳ͍ ݁ߏ͑Δʂ
͜ΜͳΫϥε͕͋ͬͯ public class MonsterParty { public string Name { get;
set; } // MonsterͷϦετɺ͜ͷϓϩύςΟ͕ϛι public List<Monster> MonsterList { get; set; } }
SelectMany ϦετͷϦετͷฏԽΛߦ͏(ͦͷ2) // ύʔςΟʔͷϦετ List<MonsterParty> monsterPartyList = LoadMonsterPartyList (); //
Ϟϯελʔͷγʔέϯεʹ IEnumerable<Monster> monsters = monsterPartyList .SelectMany ((MonsterParty party) => party.MonsterList);
Dis$nct ॏෳΛআ͘
Dis$nct ॏෳΛআ͘ // γϯϓϧͳྫ int[] ids = new int[]{0, 1,
1, 2, 2, 2, 3, 3, 4}; // ॏෳ͕আ͔Ε͍ͯΔ IEnumerable<int> distinctedIds = ids.Distinct ();
Dis$nct ॏෳΛআ͘ // ύʔςΟʔʹಉ͡Ϟϯελʔ͍ͳ͍͕ɺଞͷύʔςΟʔʹಉ͡Ϟϯελʔ͕͍Δ List<MonsterParty> monsterPartyList = LoadMonsterPartyList (); //
ॏෳͷແ͠ϞϯελʔͷIdͷγʔέϯεΛ࡞Δ IEnumerable<int> monsterIds = monsterPartyList // Ϟϯελʔͷγʔέϯεʹͯ͠ .SelectMany (party => party.MonsterList) // Idͷγʔέϯεʹͯ͠ .Select (monster => monster.Id) // ॏෳΛഉআ͢Δ .Distinct ();
ͪͳΈʹLINQʹ࣍ͷΑ͏ͳू߹ԋࢉΛߦ͏ͷ͋Γ·͢ ࠩू߹ΛٻΊΔ!Except ू߹ΛٻΊΔ!Union ੵू߹ΛٻΊΔ!Intersect
Join ೋͭͷγʔέϯεΛΩʔͰಥ߹
PlayLogΫϥεͱStageDataΫϥε͔ΒɺPlayDataΛ࡞Γ͍ͨ public class PlayLog { // StageͷIdͷΈͰStageͷใͳ͍ public int StageId
{ get; set; } public int Score { get; set; } /* தུ */ } public class StageData { public int Id { get; set; } public string Name { get; set; } /* தུ */ }
PlayLogΫϥεͱStageDataΫϥε͔ΒɺPlayDataΛ࡞Γ͍ͨ public class PlayData { // StageDataΫϥεͷ໊લ͔Β public string StageName
{ get; set; } // PlayLogΫϥεͷScore͔Β public int Score { get; set; } /* தུ */ }
List<PlayLog>ͳplayLogListͱ List<StageData>ͳstageDataList͔Β PlayDataͷγʔέϯεΛ࡞Γ͍ͨ
foreachͳྫ // foreachͳྫɺ͍ List<PlayData> playDataList = new List<PlayData> (); foreach(PlayLog
playLog in playLogList) { StageData targetStageData = null; foreach(StageData stageData in stageDataList) { if (playLog.StageId == stageData.Id) { targetStageData = stageData; break; // nullͳͭͳ͍ΑͶͱ͍͏ίʔυ } } playDataList.Add (new PlayData { StageName = targetStageData.Name, Score = playLog.Score, }); }
LINQར༻ͳྫ // LINQར༻ͳྫɺ·͍ͩ List<PlayData> playDataList = new List<PlayData> (); foreach(PlayLog
playLog in playLogList) { StageData stageData = stageDataList.First (stage => stage.Id == playLog.StageId); playDataList.Add (new PlayData { StageName = stageData.Name, Score = playLog.Score, }); }
Joinͷྫ IEnumerable<PlayData> playDatas = playLogList.Join ( stageDataList, (PlayLog playLog) =>
playLog.StageId, // PlayLogͷΩʔ (StageData stageData) => stageData.Id, // StageDataͷΩʔ // ೋͭͷΩʔ͕ἧͬͨPlayLogͱStageData͕དྷΔ (PlayLog playLog, StageData stageData) => new PlayData { StageName = stageData.Name, Score = playLog.Score, });
ͯ͞
LINQͱͷ͖͍ͭ͋ํ • ΤϥʔʹͳΒͳ͍ϥΠϒϥϦΛ࡞Δor͏ • ΤϥʔʹͳΔϝιουor͍ํͰΘͳ͍ • Editor֦ுͷΈͰ͏ • ΤϥʔʹͳΔ͕Δ·Ͱͭ
Կ͔࣭͋Γ·͢ʁ
ͯ͞ɺࠓͷඪͷ͓͞Β͍Ͱ͢
ࠓͷඪ Έͳ͞Μʹ • LINQΛ͏ͱίʔυ͕៉ྷʹͳΔ͜ͱΛͬͯΒ͏ • LINQΛ͑Δ׆༻γʔϯ͕ଟ͍͜ͱΛͬͯΒ͏ • LINQΛͬͯΈΑ͏ɾษڧͯ͠ΈΑ͏ͱࢥͬͯΒ͏ • LINQʹ͜Μͳศརͳϝιου͋ͬͨΜͩͱͬͯΒ͏
͍͔͕ͩͬͨͰ͠ΐ͏͔ʁ LINQͬͯΈͨ͘ͳΓ·͔ͨ͠ʁ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ @RyotaMurohoshi