【发布时间】:2012-07-12 06:27:56
【问题描述】:
我遇到了关于使用组合框删除数据的问题。该错误提示我不知道如何解决它。有人可以帮我吗?
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 部分显示错误
运算符“==”不能应用于“字符串”和“短”类型的操作数。”。
您的帮助将不胜感激。
以上问题的答案如下
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.");
}
}
}
【问题讨论】:
-
如果我没记错的话,在 linq 中应该是单个“=”来表示相等?
-
@WaqarJanjua 不,你错了 :)
-
是的 sing "=" 无法工作,这就是我被卡住的原因。