データベースから入力したディクショナリがあります。
返されるレコードが 1 つだけの場合、KeyValuePair にアクセスするにはどうすればよいですか?
私はこのコードを持っています:
Dictionary<int, string> CandidatePlatypiDict = PlatypusID > 0 ? DuckbillData.GetPlatypusById(PlatypusID) : DuckbillData.GetCandidatePlatypiFromName(SearchVal);
KeyValuePair<int, String> kvp;
...これにより、ディクショナリに問題なく入力されます。複数の KVP がある場合、それらを DGV に表示してから、次のように kvp に割り当てます。
DataGridViewRow selectedRow = dataGridViewPlatypi.Rows[selectedrowindex];
int i = Convert.ToInt32(selectedRow.Cells["DuckBillAccountNum"].Value);
string s = Convert.ToString(selectedRow.Cells["DuckBillName"].Value);
this.PlatypiKVP = new KeyValuePair<int, string>(i, s);
...しかし、レコードが 1 つしかない場合は、DGV に値を表示する必要はありません。実際に選択する必要がないため、その値を使用するだけです。しかし、これまでのところ、必要な値 kvp.Key と kvp.Value を割り当てる方法について空白を描いています。
if (CandidatePlatypiDict.Count == 1)
{
//kvp.Key = CandidatePlatypiDict[0]. strike 1
//kvp.Key = CandidatePlatypiDict.Keys[0] strike 2
// ???
}