C# 使用linq删除的代码出错

C# 使用linq删除的代码出错,c#,mysql,linq,combobox,C#,Mysql,Linq,Combobox,可能重复: 我遇到一个关于使用组合框删除数据的问题。这个错误促使我不知道如何解决它。有人能帮我吗 private void btnDel_Click(object sender, EventArgs e) { using (testEntities Setupctx = new testEntities()) { var Lo = Convert.ToInt16(cbLocationData.SelectedValue);

可能重复:

我遇到一个关于使用组合框删除数据的问题。这个错误促使我不知道如何解决它。有人能帮我吗

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.");
        }
    }
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.");
            }
        }
    }
这意味着您无法比较delLocation.Location1和Lo,因为它们属于不同的数据类型。尝试:

where delLocation.Location1.Equals(Lo.ToString())
这意味着您无法比较delLocation.Location1和Lo,因为它们属于不同的数据类型。尝试:

where delLocation.Location1.Equals(Lo.ToString())
显然,Location1是一个字符串,不能用==直接与short进行比较。尝试以下操作,而不是将Lo转换为短字符,然后再转换回字符串:

显然,Location1是一个字符串,不能用==直接与short进行比较。尝试以下操作,而不是将Lo转换为短字符,然后再转换回字符串:


错误表示您正在尝试将字符串与Int16进行比较。因为我们已经知道Lo是Int16,所以delLocation.Location1必须是字符串。要解决这个问题,请删除Convert.ToInt16,因为dropdownlist的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.");
    }
}
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.");
        }
    }
}
使现代化 如果您得到的错误序列不包含任何元素,这意味着您的查询没有返回任何结果,并且您不能对空序列执行单个操作。您可以使用SingleOrDefault,然后检查该值是否为null,如下所示:

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.");
    }
}
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.");
        }
    }
}

错误表示您正在尝试将字符串与Int16进行比较。因为我们已经知道Lo是Int16,所以delLocation.Location1必须是字符串。要解决这个问题,请删除Convert.ToInt16,因为dropdownlist的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.");
    }
}
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.");
        }
    }
}
使现代化 如果您得到的错误序列不包含任何元素,这意味着您的查询没有返回任何结果,并且您不能对空序列执行单个操作。您可以使用SingleOrDefault,然后检查该值是否为null,如下所示:

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.");
    }
}
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.");
        }
    }
}


如果我没有错的话,它应该是单身=在林克平等?@WaqarJanjua没有,你错了:Yea sing=不能工作,这就是我被卡住的原因。如果我没有错的话,它应该是单身=在林克平等?@WaqarJanjua没有,你错了:Yea sing=无法工作,这就是我被卡住的原因。我尝试了你的方法,但仍然有错误提示“LINQ to Entities”无法识别你的方法…@Philemon你现在可以尝试编辑的答案吗?仍然提示我相同的错误。你能检查delLocation.Location1或Lo是否为空吗?我尝试了你的方法,但仍然有错误提示“LINQ to Entities”无法识别您的方法…@Philemon您现在可以尝试编辑的答案吗?仍然提示我相同的错误。您可以检查delLocation.Location1或Lo是否为null吗?这很可能会给出错误的结果。cbLocationData.SelectedValue是一个对象。如果不将其转换为字符串,则是在比较引用,而不是字符串。@Kristof我也尝试过您的方法,但有一个错误提示我“序列不包含元素”。@ErenErsönmez知道如何完成此代码吗?我现在真的迷路了。@ErenErsönmez我已经试过了,错误提示“序列不包含元素。这是因为DeleteLocation为null。”。这意味着没有Location1属性等于cbLocationData的SelectedValue的位置。这很可能会给出错误的结果。cbLocationData.SelectedValue是一个对象。如果不将其转换为字符串,则是在比较引用,而不是字符串。@Kristof我也尝试过您的方法,但有一个错误提示我“序列不包含元素”。@ErenErsönmez知道如何完成此代码吗?我现在真的迷路了。@ErenErsönmez我已经试过了,错误提示“序列不包含元素。这是因为DeleteLocation为null。”。这意味着没有Location1属性等于cbLocationData的SelectedValue的位置。它们会提示我错误序列不包含元素。Setupctx.locations中是否真的没有具有匹配字符串值的项?如果它们可能匹配但大小写不同,您可以尝试使用String.Compare而不是==运算符。String.Compare具有重载,允许您忽略某些差异。它们会提示我错误序列不包含任何元素。Setupctx.locations中是否确实没有具有匹配字符串值的项?如果它们可能匹配但大小写不同,您可以尝试使用String.Compare而不是==运算符。Compare具有重载,可以忽略某些差异。