Avoid saving duplicate data

1

In my project I have a rule that does not save and does not show in Grid duplicate data. For example, on the system I read an XML and save the tags information to a table in my database so that this information is displayed in a Grid .

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

So I made a logic so that I did not show the Grid this duplicate 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;

In other words, if there are data in these tags that are already in Grid , that data is not shown ...

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

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

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

Is there any way I can do this check and show no error, neither of SQL Server nor C# ? That is, just saving the die once?

    
asked by anonymous 29.06.2015 / 14:23

1 answer

4

If data read from xml is stored in List<T> before being written to the database then delete the duplicates from the list before writing.

One simple way to do this is to use the IQueryable.Distinct .

In order for this method to distinguish a duplicate row from its class, you must implement the 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 Grid and save to the bank.

    
29.06.2015 / 15:56