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は であり、 usingstringと直接比較することはできません。に変換してからに戻す代わりに、次を試してください。short==Loshortstring

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()次のように使用して、値が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).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 に答える