重複の可能性:
linq を使用して削除するためのコードのエラー
コンボ ボックスを使用したデータの削除に関して問題が発生しました。このエラーにより、解決方法がわかりません。誰でもそれについて私を助けることができますか?
private void btnDel_Click(object sender, EventArgs e)
{
using (testEntities Setupctx = new testEntities())
{
var Lo = Convert.ToInt16(cbLocationData.SelectedValue);
var DeleteLocation = (from delLocation in Setupctx.locations
where delLocation.Location1 == Lo
select delLocation).Single();
Setupctx.DeleteObject(DeleteLocation);
Setupctx.SaveChanges();
this.Delete_Location_Load(null, EventArgs.Empty);
MessageBox.Show("Selected Shift Timing Has Been Deleted.");
}
}
where delLocation.Location1 == Lo
エラーが表示されている部分
演算子 '==' は、型 'string' および 'short' のオペランドには適用できません。".
どうぞよろしくお願いいたします。
上の質問の答えは以下
private void btnDel_Click(object sender, EventArgs e)
{
using (testEntities Setupctx = new testEntities())
{
string selectLo = cbLocationData.SelectedItem.ToString();
var DeleteLocation = (from delLocation in Setupctx.locations
where delLocation.Location1 == selectLo
select delLocation).SingleOrDefault();
if (DeleteLocation != null)
{
Setupctx.DeleteObject(DeleteLocation);
Setupctx.SaveChanges();
cbLocationData.SelectedIndex = -1;
this.Delete_Location_Load(null, EventArgs.Empty);
MessageBox.Show("Selected Shift Timing Has Been Deleted.");
}
}
}