SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
References
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<pre data-priority="2" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(249, 249, 249); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; color: rgba(0, 0, 0, 0.8); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true"><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255); display: block; white-space: pre-wrap; overflow-wrap: anywhere; padding: 8px 16px 16px;"><span style="color: rgb(215, 58, 73);">using</span> System; <span style="color: rgb(215, 58, 73);">using</span> System.Collections.Generic; <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">Payment</span> { <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span> PaymentId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(106, 115, 125);">// 自動生成される</span> <span style="color: rgb(215, 58, 73);">public</span> DateTime Posted { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">bool</span> InOut { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? SubjectNameId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">string</span> ItemName { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">string</span> PaymentName { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? PaymentTypeId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? PaymentMethodId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? IncomeCategoryId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span>? IncomeTypeId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">decimal</span> Amount { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">Program</span> { <span style="color: rgb(106, 115, 125);">// 共通のTryGetValueラッパー(見つからなければnull)</span> <span style="color: rgb(215, 58, 73);">private</span> <span style="color: rgb(215, 58, 73);">static</span> <span style="color: rgb(227, 98, 9);">int</span>? GetId(Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> dict, <span style="color: rgb(227, 98, 9);">string</span> key) { <span style="color: rgb(215, 58, 73);">return</span> dict.TryGetValue(key, <span style="color: rgb(215, 58, 73);">out</span> <span style="color: rgb(215, 58, 73);">var</span> <span style="color: rgb(215, 58, 73);">value</span>) && <span style="color: rgb(215, 58, 73);">value</span> != <span style="color: rgb(0, 92, 197);">0</span> ? <span style="color: rgb(215, 58, 73);">value</span> : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">static</span> <span style="color: rgb(215, 58, 73);">void</span> <span style="color: rgb(111, 66, 193);">Main</span>() { <span style="color: rgb(227, 98, 9);">int</span> thisYear = DateTime.Now.Year; <span style="color: rgb(227, 98, 9);">int</span> previousMonth = DateTime.Now.Month - <span style="color: rgb(0, 92, 197);">1</span>; <span style="color: rgb(215, 58, 73);">if</span> (previousMonth == <span style="color: rgb(0, 92, 197);">0</span>) { previousMonth = <span style="color: rgb(0, 92, 197);">12</span>; thisYear--; } <span style="color: rgb(106, 115, 125);">// 各Id辞書</span> <span style="color: rgb(215, 58, 73);">var</span> subjectNameDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"家計"</span>, <span style="color: rgb(0, 92, 197);">1</span> }, { <span style="color: rgb(3, 47, 98);">"事業"</span>, <span style="color: rgb(0, 92, 197);">2</span> } }; <span style="color: rgb(215, 58, 73);">var</span> paymentTypeDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"予算"</span>, <span style="color: rgb(0, 92, 197);">10</span> }, { <span style="color: rgb(3, 47, 98);">"支出"</span>, <span style="color: rgb(0, 92, 197);">20</span> } }; <span style="color: rgb(215, 58, 73);">var</span> paymentMethodDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"現金"</span>, <span style="color: rgb(0, 92, 197);">100</span> }, { <span style="color: rgb(3, 47, 98);">"銀行振込"</span>, <span style="color: rgb(0, 92, 197);">200</span> } }; <span style="color: rgb(215, 58, 73);">var</span> incomeCategoryDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"給与"</span>, <span style="color: rgb(0, 92, 197);">300</span> }, { <span style="color: rgb(3, 47, 98);">"副業"</span>, <span style="color: rgb(0, 92, 197);">400</span> } }; <span style="color: rgb(215, 58, 73);">var</span> incomeTypeDict = <span style="color: rgb(215, 58, 73);">new</span> Dictionary<<span style="color: rgb(227, 98, 9);">string</span>, <span style="color: rgb(227, 98, 9);">int</span>> { { <span style="color: rgb(3, 47, 98);">"定期"</span>, <span style="color: rgb(0, 92, 197);">500</span> }, { <span style="color: rgb(3, 47, 98);">"臨時"</span>, <span style="color: rgb(0, 92, 197);">600</span> } }; <span style="color: rgb(106, 115, 125);">// 複数のPaymentデータ定義(キー名と金額)</span> <span style="color: rgb(215, 58, 73);">var</span> paymentData = <span style="color: rgb(215, 58, 73);">new</span>[] { <span style="color: rgb(215, 58, 73);">new</span> { SubjectKey = <span style="color: rgb(3, 47, 98);">"家計"</span>, TypeKey = <span style="color: rgb(3, 47, 98);">"予算"</span>, MethodKey = <span style="color: rgb(3, 47, 98);">"現金"</span>, CategoryKey = <span style="color: rgb(3, 47, 98);">"給与"</span>, IncomeTypeKey = <span style="color: rgb(3, 47, 98);">"定期"</span>, Amount = <span style="color: rgb(0, 92, 197);">268000</span>m, ItemName = <span style="color: rgb(3, 47, 98);">"今月予算"</span> }, <span style="color: rgb(215, 58, 73);">new</span> { SubjectKey = <span style="color: rgb(3, 47, 98);">"事業"</span>, TypeKey = <span style="color: rgb(3, 47, 98);">"支出"</span>, MethodKey = <span style="color: rgb(3, 47, 98);">"銀行振込"</span>, CategoryKey = <span style="color: rgb(3, 47, 98);">"副業"</span>, IncomeTypeKey = <span style="color: rgb(3, 47, 98);">"臨時"</span>, Amount = <span style="color: rgb(0, 92, 197);">150000</span>m, ItemName = <span style="color: rgb(3, 47, 98);">"事業支出"</span> } }; <span style="color: rgb(215, 58, 73);">var</span> payments = <span style="color: rgb(215, 58, 73);">new</span> List<Payment>(); <span style="color: rgb(215, 58, 73);">foreach</span> (<span style="color: rgb(215, 58, 73);">var</span> data <span style="color: rgb(215, 58, 73);">in</span> paymentData) { payments.Add(<span style="color: rgb(215, 58, 73);">new</span> Payment { Posted = <span style="color: rgb(215, 58, 73);">new</span> DateTime(thisYear, previousMonth, <span style="color: rgb(0, 92, 197);">1</span>), InOut = <span style="color: rgb(0, 92, 197);">true</span>, SubjectNameId = GetId(subjectNameDict, data.SubjectKey), ItemName = data.ItemName, PaymentName = <span style="color: rgb(0, 92, 197);">null</span>, PaymentTypeId = GetId(paymentTypeDict, data.TypeKey), PaymentMethodId = GetId(paymentMethodDict, data.MethodKey), IncomeCategoryId = GetId(incomeCategoryDict, data.CategoryKey), IncomeTypeId = GetId(incomeTypeDict, data.IncomeTypeKey), Amount = data.Amount }); } <span style="color: rgb(106, 115, 125);">// 確認出力</span> <span style="color: rgb(215, 58, 73);">foreach</span> (<span style="color: rgb(215, 58, 73);">var</span> p <span style="color: rgb(215, 58, 73);">in</span> payments) { Console.WriteLine(<span style="color: rgb(3, 47, 98);">$"Posted: <span style="color: rgb(36, 41, 46);">{p.Posted:yyyy-MM-dd}</span>, SubjectNameId: <span style="color: rgb(36, 41, 46);">{p.SubjectNameId}</span>, Amount: <span style="color: rgb(36, 41, 46);">{p.Amount}</span>"</span>); } } } </code></pre><h3 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: inline-block; font: 400 20px / 22px roboto, helvetica, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">改良点</h3><ol style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">共通化</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">GetId</code> メソッドで <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">TryGetValue</code> の処理を一元化し、見つからない場合は <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">null</code> を返す。</p></li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">ループ生成</strong><br>複数の <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Payment</code> を匿名型配列 <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">paymentData</code> に定義し、ループで <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">List<Payment></code> に追加。</p></li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert;"><strong style="font-weight: 700;">可読性向上</strong><br>辞書キーと金額をまとめて管理できるため、コードが短くなり保守性が高い。</p></li></ol><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">この形なら、辞書のキーや金額を追加するだけで新しい <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Payment</code> を簡単に生成できます。<br>もし必要なら、この <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">paymentData</code> を外部設定ファイルやDBから読み込む形にもできます。</p><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">次はこのコードを <strong style="font-weight: 700;">ASP.NET Core MVC の Controller 内で使える形</strong> に書き換えたバージョンも作れますが、それも提示しますか?<br>そうすれば実際のアプリに組み込みやすくなります。</p><p data-f-id="pbf" style="text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;">Powered by <a href="https://www.froala.com/wysiwyg-editor?pb=1" title="Froala Editor">Froala Editor</a></p>
コピーする
添付画像
画像を変更
Back to List
//検索文:Editor内でHTMLコードをコピーさせる