使用linq删除代码的错误

我遇到了使用combobox删除数据的问题。 错误提示我,我不知道如何解决它。 任何人都可以帮助我吗?

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 LoadLocation() { using (testEntities Setupctx = new testEntities()) { var storeLocation = (from vL in Setupctx.locations select new { Location1 =vL.Location1 } ); cbLocationData.DataTextField = "Location1"; cbLocationData.DataSource = storeLocation; cbLocationData.DataBind(); } } 

然后在你的页面加载(asp.net)/表单加载(winform)添加:

  LoadLocation(); 

希望这有帮助。

问候

 private void cbLocationData_SelectedIndexChanged(object sender, EventArgs e) { using (testEntities Setupctx = new testEntities()) { var storeLocation = (from vL in Setupctx.locations where vL.Location1 == vL.Location1 select vL.Location1); foreach (var locationData in storeLocation) { cbLocationData.Items.Add(locationData.ToString()); } } } 

是否有可能将locationData设置为tostring()或convert(),具体取决于数据类型? 一切看起来都应该正常工作。

您的事件是在您尝试填写的cb的SelectedindexChanged上触发的。 尝试将其放入页面加载或更合适的位置?

我认为你把你的代码放在了错误的地方..你只需在相同的组合选择更改(cbLocationData_SelectedIndexChanged)上添加项目,这是错误的
将您的代码放在其他适当的位置,其中添加项目不属于同一事件