Avoid saving duplicate data

Asked

Viewed 1,342 times

1

In my project I have a rule that is not to save and not to show in Grid duplicated data. For example, in the system I read a XML and with the exception of tags in a table in my database so that this information is shown in a Grid.

Only every time I do that reading, the data is saved twice. I mean, if I’ve read it once and it’s been saved, for example, the name 'John' in this first reading, if I read it again XML, the name is saved again, and so on, how many times I read the XML.

Then I made a logic not to show in Grid these duplicated data, which is this:

if (Convert.ToString(GetValueOrDefault(elmCertidao.Element("nome_tag_1"))) == objeto.Propriedades["Coluna1"].Valor &&
                                    Convert.ToString(GetValueOrDefault(elmCertidao.Element("nome_tag_2"))) == objeto.Propriedades["Coluna2"].Valor &&
                                    Convert.ToString(GetValueOrDefault(elmCertidao.Element("nome_tag_3"))) == objeto.Propriedades["Coluna3"].Valor)
                                    continue;

That is, if there is any data in these tags who are already in the Grid, these data are not shown...

So far beauty, but it turns out that duplicated data is saved in the database table, and that’s not what I want.

I want you not to show in Grid or save duplicate data.

This question here from Sopt give me an idea of what to do. But if I put the fields as Unique, every time I read the XML, and the inconsistency is verified, will give error in SQL Server saying that the data already exists.

Is there any way I can do this check and not show error, nor SQL Server nor in the C# ? That is, saving only once the die ?

  • Why read more than once the xml?

  • Because in the XML may contain a tag that groups records and repeats it. Type an NF-e(Here in Brazil). It has a tag that groups several others with records, and this tag that it groups repeats several times. And if I need to read this XML more than once, I don’t want to save data that has already been saved.

  • To avoid repetitions use Unique. Before recording, read to check if the record already exists or record and do the catch error. If the possibility of repeats is large use the first, if small use the second.

  • Is there any way to do this via c# and without showing error ? Or only this way ?

  • Both forms are via C# and neither shows the error.

  • It would have to put in an answer an example ?

  • You enter the read data from xml at some Collection before recording in the bank?

  • I populate in an object that represents my table in the database, show the data in Grid and then saved in the bank.

  • This object is a List<T> where T represents a record of the table?

  • Exactly that! But in case you can do it in a generic way, I adapt my reality.

  • To give you an answer I need to know which property of your class must be unique.

  • The Coluna1, Coluna2 and Coluna3 that are there in the if. That is, person’s name, father’s name and mother’s name.

  • @Érikthiago, I think you should treat at the time of recording. A simple check routine to know if there is or not in the BD and then release the recording routine or not. As said the extension.

  • @pnet you would have an example of code that does this kind of routine?

Show 9 more comments

1 answer

4


If the data read from xml are stored in a List<T> before being saved to the bank then delete duplicates from the list before saving.

A simple way to do this is to use the method Iqueryable.Distinct.

In order for this method to distinguish what is a duplicated line its class will need to implement the interface IEquatable<T>:

public class SuaClasse : IEquatable<SuaClasse>
{

    //Suas Propriedades
    public string Nome { get; set; }
    public string NomePai { get; set; }
    public string NomeMae { get; set; }
    ....
    .....
    //Seus Métodos
    ....
    ....

    //Implementação da Interface
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        SuaClasse objAs = obj as SuaClasse;
        if (objAs == null) return false;
        else return Equals(objAs);
    }

    public override int GetHashCode()
    {
        return Nome.GetHashCode() ^ NomePai.GetHashCode() ^ NomeMae.GetHashCode();
    }

    public bool Equals(SuaClasse other)
    {
        if(other == null)return false;
        return Nome.Equals(other.Nome) &&
               NomePai.Equals(other.NomePai) &&
               NomeMae.Equals(other.NomeMae);
    }
}

To get the list without duplicates use:

var ListaSemDuplicados = suaLista.Distinct().ToList();

Use ListaSemDuplicados to popular the Grid and to save in the bank.

  • Well, I think the ramaral has already answered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.