可能重复:
使用 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.");
}
}
}