【问题标题】:Errors with codes for deleting using linq使用 linq 删除代码的错误
【发布时间】:2012-07-11 18:47:55
【问题描述】:

我遇到了关于使用组合框删除数据的问题。该错误提示我不知道如何解决它。有人可以帮我吗?

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 部分显示以下错误

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

我们将不胜感激。

【问题讨论】:

  • 您是否尝试过单步执行代码以确保其进入您的 foreach 循环?
  • 您的事件在您尝试填充的 cb 的 SelectedindexChanged 上触发。试试把它放在页面加载或更合适的地方?
  • @Ghost 谢谢提醒。我将我的代码移到 Page_Load 并且它工作得很好。
  • 我认为您将代码放在错误的位置.. 我同意@Ghost

标签: c# mysql linq combobox


【解决方案1】:

创建一个类似这样的方法:

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();

        }
}

然后在你的页面 load(asp.net)/form Load(winform) 添加:

           LoadLocation();

希望对您有所帮助。

问候

【讨论】:

    【解决方案2】:
    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(),具体取决于数据类型?一切看起来都应该正常工作。

    【讨论】:

    • 我尝试了你的方法,但组合框似乎仍然没有任何数据。
    【解决方案3】:

    您的事件在您尝试填充的 cb 的 SelectedindexChanged 上触发。试试把它放在页面加载中或更合适的地方?

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        • 2017-10-31
        相关资源
        最近更新 更多