【问题标题】:Error with codes for deleting using linq [duplicate]使用 linq 删除代码时出错 [重复]
【发布时间】:2012-07-12 06:27:56
【问题描述】:

可能重复:
Errors with codes for deleting using 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 部分显示错误

运算符“==”不能应用于“字符串”和“短”类型的操作数。”。

您的帮助将不胜感激。

以上问题的答案如下

 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 "=" 无法工作,这就是我被卡住的原因。

标签: c# mysql linq combobox


【解决方案1】:

显然Location1string,不能直接与使用==short 进行比较。不要将Lo 转换为short,然后再转换回string,请尝试:

var Lo = (string)cbLocationData.SelectedValue;

【讨论】:

  • 他们提示我一个错误“序列不包含元素。”
  • Setupctx.locations 中是否真的没有具有匹配字符串值的项目?如果它们可能匹配但大小写不同,您可以尝试使用String.Compare 而不是== 运算符。 String.Compare 有让你忽略一些差异的重载。
【解决方案2】:

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

where delLocation.Location1.Equals(Lo.ToString())

【讨论】:

  • 我试过你的方法,但仍然有错误提示'LINQ to Entities'无法识别你的方法......'
  • @Philemon 您现在可以尝试编辑后的答案吗?
  • 仍然提示我同样的错误。
  • 你能检查delLocation.Location1Lo是否为空吗?
【解决方案3】:

错误表明您正在尝试将字符串与 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.");
        }
    }
}

【讨论】:

  • 这很可能会给出错误的结果。 cbLocationData.SelectedValue 是一个对象。如果您不将其转换为字符串,那么您是在比较引用,而不是字符串。
  • @Kristof 我也尝试过你的方法,但出现错误提示我“序列不包含元素”。
  • @ErenErsönmez 知道如何完成此代码吗?我现在真的迷路了。
  • @ErenErsönmez 我试过了,错误提示我“序列不包含元素”。
  • 那是因为 DeleteLocation 为空。这意味着没有 Location1 属性等于 cbLocationData 的 SelectedValue 的位置。
猜你喜欢
  • 2012-07-11
  • 2020-03-18
  • 2012-07-02
  • 1970-01-01
  • 2020-04-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多