简体   繁体   English

删除具有两个数据库的表中的记录时出错

[英]Error while deleting record in a table with two databases

First I want to report that because of my English this is a machine translation from Spanish to English, Please excuse for mistakes that may have been in the translation. 首先,我想报告一下,由于我的英语,这是一个从西班牙语到英语的机器翻译,请原谅翻译中可能存在的错误。

I have a problem in a small program I'm doing and I would like you could help me: I have a database with two tables customer (pelo2 hair), I access them in two different windows at first had a delete button in the first window, to remove all the selected customer record, but having the other window all customer data window and change it gives me the following error: 我在一个小程序中遇到问题,我希望你可以帮助我:我有一个数据库,有两个表客户(pelo2发),我在两个不同的窗口访问它们,第一个在第一个有一个删除按钮窗口,删除所有选定的客户记录,但让其他窗口所有客户数据窗口并更改它给我以下错误:

Unable to cast object of type 'System.Data.Linq.DataQuery`1[Peluqueria.Pelos2]' to type 'Peluqueria.Pelos2'.

this occasionally happen in the event of the button Eliminar 这偶尔发生在按钮Eliminar的情况下

        private void Eliminar_Click(object sender, RoutedEventArgs e)
    {
        MessageBoxResult result = MessageBox.Show("Seguro que quieres eliminar este cliente?", "Confirmar",
        MessageBoxButton.YesNo, MessageBoxImage.Information);
        if (result == MessageBoxResult.Yes)
        {
            Pelo guar = (Pelo)Application.Current.Properties["seleccionar"];


                var verTrabajo = (from o in baseDeDatos.Pelos2s
                                  where o.num == guar.num
                                  select o);


                baseDeDatos.Pelos2s.DeleteOnSubmit((Pelos2)verTrabajo);
                baseDeDatos.SubmitChanges();
                actualizarDatos();

        }
    }          

specifically in the row: 特别是在行中:

baseDeDatos.Pelos2s.DeleteOnSubmit((Pelos2)verTrabajo);

I must say that I get the same error, even with the baseDeDatos.Pelos. 我必须说我得到了同样的错误,即使使用baseDeDatos.Pelos。 Can I help you can know what I'm doing wrong? 我可以帮你知道我做错了什么吗? Can it be done differently? 它能以不同的方式完成吗? Thanks for your help. 谢谢你的帮助。

This line: 这一行:

var verTrabajo = (from o in baseDeDatos.Pelos2s
                                  where o.num == guar.num
                                  select o);

returns a collection. 返回一个集合。 It may well be that the collection contains just the one record you're expecting to delete but you need to be specific. 很可能该集合只包含您希望删除的一条记录,但您需要具体。

If you're expecting it to return one result so you can use that result to delete try using SingleOrDefault() : 如果您希望它返回一个结果,那么您可以使用该结果删除尝试使用SingleOrDefault()

var verTrabajo = (from o in baseDeDatos.Pelos2s
                                  where o.num == guar.num
                                  select o).SingleOrDefault();

Make sure you check if it's not null before you use it for the delete. 在将其用于删除之前,请确保检查它是否为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM