SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
References
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<p data-bm="48" 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;" data-pasted="true">以下に <strong style="font-weight: 700;">ASP.NET Core MVC</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);">List<Payment></code> に追加する方法です。</p><h3 data-bm="49" 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><pre data-priority="2" data-bm="50" 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;"><div data-bm="55" style="display: flex; height: 28px; flex-direction: row; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid rgba(0, 0, 0, 0.1); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(243, 249, 254);"><h5 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 0px; padding: revert; font-style: normal !important; font-variant: normal !important; font-weight: 400 !important; font-stretch: 100% !important; line-height: 18px; font-family: roboto, helvetica, sans-serif !important; font-optical-sizing: auto !important; font-size-adjust: none !important; font-kerning: auto !important; font-feature-settings: normal !important; font-variation-settings: normal !important; font-language-override: normal !important; font-size: 16px; display: inline-block; color: rgb(11, 73, 127);">Csharp</h5><div style="height: 28px;"><button type="button" tabindex="" title="Copy code" style="box-sizing: border-box; margin: 0px; padding: 0px; font: 400 13px / 20px roboto, helvetica, sans-serif; background-color: rgb(225, 240, 253); border: 1px solid rgba(0, 0, 0, 0); display: grid; color: rgb(0, 0, 0) !important; grid-auto-flow: column; align-items: center; cursor: pointer; text-decoration: none !important; border-radius: 9999px; block-size: 28px; gap: 2px; padding-inline: 10px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; min-inline-size: 0px; max-inline-size: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: clip; max-block-size: calc(1lh); padding-inline: 2px;">Copy code</div></button></div></div><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(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);">// TryGetValueで安全に取得</span> <span style="color: rgb(227, 98, 9);">int</span> subjectId; <span style="color: rgb(227, 98, 9);">int</span> paymentTypeId; <span style="color: rgb(227, 98, 9);">int</span> paymentMethodId; <span style="color: rgb(227, 98, 9);">int</span> incomeCategoryId; <span style="color: rgb(227, 98, 9);">int</span> incomeTypeId; subjectNameDict.TryGetValue(<span style="color: rgb(3, 47, 98);">"家計"</span>, <span style="color: rgb(215, 58, 73);">out</span> subjectId); <span style="color: rgb(106, 115, 125);">// 見つからなければ0</span> paymentTypeDict.TryGetValue(<span style="color: rgb(3, 47, 98);">"予算"</span>, <span style="color: rgb(215, 58, 73);">out</span> paymentTypeId); paymentMethodDict.TryGetValue(<span style="color: rgb(3, 47, 98);">"現金"</span>, <span style="color: rgb(215, 58, 73);">out</span> paymentMethodId); incomeCategoryDict.TryGetValue(<span style="color: rgb(3, 47, 98);">"給与"</span>, <span style="color: rgb(215, 58, 73);">out</span> incomeCategoryId); incomeTypeDict.TryGetValue(<span style="color: rgb(3, 47, 98);">"定期"</span>, <span style="color: rgb(215, 58, 73);">out</span> incomeTypeId); <span style="color: rgb(106, 115, 125);">// Paymentリスト</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(106, 115, 125);">// new Paymentで追加</span> 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 = subjectId != <span style="color: rgb(0, 92, 197);">0</span> ? subjectId : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>, ItemName = <span style="color: rgb(3, 47, 98);">"今月予算"</span>, PaymentName = <span style="color: rgb(0, 92, 197);">null</span>, PaymentTypeId = paymentTypeId != <span style="color: rgb(0, 92, 197);">0</span> ? paymentTypeId : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>, PaymentMethodId = paymentMethodId != <span style="color: rgb(0, 92, 197);">0</span> ? paymentMethodId : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>, IncomeCategoryId = incomeCategoryId != <span style="color: rgb(0, 92, 197);">0</span> ? incomeCategoryId : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>, IncomeTypeId = incomeTypeId != <span style="color: rgb(0, 92, 197);">0</span> ? incomeTypeId : (<span style="color: rgb(227, 98, 9);">int</span>?)<span style="color: rgb(0, 92, 197);">null</span>, Amount = <span style="color: rgb(0, 92, 197);">268000</span> }); <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}</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 data-bm="51" 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 data-bm="52" 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);">Dictionary<string, int></code> のようにキーを名前、値をIDにする。</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;">TryGetValueの安全性</strong><br><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);">out</code> 変数にデフォルト値(<code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">int</code>なら0)を入れる。</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;">null許容型への変換</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);">Id</code> プロパティが <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">int?</code>(nullable int)の場合、<br>辞書から取得した値が0なら <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;">Addの手順</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">payments.Add(new Payment { ... })</code> の中で辞書から取得した値を直接使うか、事前に変数に格納してから使う。</p></li></ol><p data-bm="53" 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);">null</code> をセットできます。<br>もし複数の <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Payment</code> を作る場合は、辞書キーを変えて同じ手順を繰り返せばOKです。</p><p data-bm="54" 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;">ループ化して複数のPaymentを自動生成する形</strong> に書き換えた例も作れますが、<br>それも提示しますか?<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コードをコピーさせる