1

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

3 回答 3

2

这意味着您无法比较 delLocation.Location1Lo因为它们属于不同的数据类型。尝试:

where delLocation.Location1.Equals(Lo.ToString())
于 2012-07-12T06:30:23.913 回答
2

显然Location1是 a string,不能与shortusing直接比较==。不要转换Lo为 a short,然后再转换为 a string,请尝试:

var Lo = (string)cbLocationData.SelectedValue;
于 2012-07-12T06:30:52.117 回答
1

该错误表明您正在尝试将字符串与 Int16 进行比较。因为我们已经知道 Lo 是一个 Int16,所以 delLocation.Location1 必须是字符串。所以要解决这个问题,你删除Convert.ToInt16()(因为SelectedValue下拉列表是字符串)是这样的:

private void btnDel_Click(object sender, EventArgs e)
{
    using (testEntities Setupctx = new testEntities())
    {
        var Lo = Convert.ToString(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.");
    }
}

更新

如果您收到错误“序列不包含元素”,这意味着您的查询没有返回任何结果,并且您不能Single()对空序列进行操作。您可以使用SingleOrDefault()然后检查该值是否为空,如下所示:

private void btnDel_Click(object sender, EventArgs e)
{
    using (testEntities Setupctx = new testEntities())
    {
        var Lo = Convert.ToString(cbLocationData.SelectedValue);
        var DeleteLocation = (from delLocation in Setupctx.locations
                              where delLocation.Location1 == Lo
                              select delLocation).SingleOrDefault();
        if (DeleteLocation != null)
        {
            Setupctx.DeleteObject(DeleteLocation);
            Setupctx.SaveChanges();
            this.Delete_Location_Load(null, EventArgs.Empty);
            MessageBox.Show("Selected Shift Timing Has Been Deleted.");
        }
    }
}
于 2012-07-12T06:32:41.713 回答