Validate that a chain is not repeated

1

I would like to know how I can validate that a string does not duplicate when inserting lines in a text file, since each line contains different information, but until now I only manage to transfer the first line by the number of records that I have stored in DataTable . I leave the code of what I am doing:

dtH1 = objConsulta.dtMuestraH1();
if (dtH1.Rows.Count > 0)
{
    ImportarH1();
    string cadena = "";
    System.IO.StreamWriter filelocaltxt = new System.IO.StreamWriter("c:\ArchivoPrueba\test.txt");
    for (int i = 0; i < dtH1.Rows.Count; i++)
    {
        cadena = H1IdRgto + "|" + H1Serie + "|3|" + H1FchExp + "|||" + H1FmaPago + "|" + H1CondPago + "|" + H1FnconDoc + "|" + H1TipoDoc + "|" + H1MtvoDesc + "|||||||||||||||||" + H1LugarExp1 + "|" + H1MtdoPago + "|" + H1Moneda + "|" + H1TC + "|||||||||||" + H1ImpLetra + "|" + H1ImprLocal + "|" + H1TipoEnv+ "||||" + H1CodImp+"|||";
        filelocaltxt.WriteLine(cadena);                    
    }
    filelocaltxt.WriteLine(cadena);
    filelocaltxt.Close();
}
    
asked by Alberto Arenas 21.04.2017 в 00:31
source

3 answers

3

You can use an auxiliary list and verify that the string does not exist in the list.

List<string> cadenas=new List<string>();

for (int i = 0; i < dtH1.Rows.Count; i++)
{
    cadena = H1IdRgto + "|" + H1Serie + "|3|" + H1FchExp + "|||" + H1FmaPago + "|" + H1CondPago + "|" + H1FnconDoc + "|" + H1TipoDoc + "|" + H1MtvoDesc + "|||||||||||||||||" + H1LugarExp1 + "|" + H1MtdoPago + "|" + H1Moneda + "|" + H1TC + "|||||||||||" + H1ImpLetra + "|" + H1ImprLocal + "|" + H1TipoEnv+ "||||" + H1CodImp+"|||";

    if(cadenas.FirstOrDefault(s => s == cadena) == null){
        cadenas.Add(cadena);
        filelocaltxt.WriteLine(cadena);
    }
}
    
answered by 21.04.2017 в 02:11
2

What I can think of is to add each line that is written to the file in an array, verifying before writing to the file that that line does not exist in the array.

In the end, the file would be left with only the non-repeated lines (like the array that was used as an auxiliary).

The code would be something like this:

 dtH1 = objConsulta.dtMuestraH1();
    if (dtH1.Rows.Count > 0)
    {   
        List<string> cadenas=new List<string>();
        ImportarH1();
        string cadena = "";
        System.IO.StreamWriter filelocaltxt = new System.IO.StreamWriter("c:\ArchivoPrueba\test.txt");
        for (int i = 0; i < dtH1.Rows.Count; i++)
        {
            cadena = H1IdRgto + "|" + H1Serie + "|3|" + H1FchExp + "|||" + H1FmaPago + "|" + H1CondPago + "|" + H1FnconDoc + "|" + H1TipoDoc + "|" + H1MtvoDesc + "|||||||||||||||||" + H1LugarExp1 + "|" + H1MtdoPago + "|" + H1Moneda + "|" + H1TC + "|||||||||||" + H1ImpLetra + "|" + H1ImprLocal + "|" + H1TipoEnv+ "||||" + H1CodImp+"|||";

            if(cadenas.FirstOrDefault(s => s == cadena) == null){
               cadenas.Add(cadena); 
               filelocaltxt.WriteLine(cadena);
            }
        }
        filelocaltxt.Close();
    }
    
answered by 21.04.2017 в 01:11
1

The problem you have is not validating that the rows are not equal, it is that you are not loading each of the rows of your DataTable at the time of writing, if not only the first, I imagine that in the ImportarH1 method .

What you must do is modify your method in the following way (this is an example, since you do not put us as is your method at present:

public ImportarH1(int fila)
{
     H1IdRgto=dtH1.Rows[i][0].ToString();
     H1Serie=dtH1.Rows[i][1].ToString();
     H1FchExp=dtH1.Rows[i][2].ToString();
     H1FmaPago=dtH1.Rows[i][3].ToString();
     //... el resto de tus campos
}

And then, simply modify the loop where you are writing in the file:

for (int i = 0; i < dtH1.Rows.Count; i++)
{
    ImportarH1(i); //aqui cargas los datos de cada fila
    cadena = H1IdRgto + "|" + H1Serie + "|3|" + H1FchExp + "|||" + H1FmaPago + "|" + H1CondPago + "|" + H1FnconDoc + "|" + H1TipoDoc + "|" + H1MtvoDesc + "|||||||||||||||||" + H1LugarExp1 + "|" + H1MtdoPago + "|" + H1Moneda + "|" + H1TC + "|||||||||||" + H1ImpLetra + "|" + H1ImprLocal + "|" + H1TipoEnv+ "||||" + H1CodImp+"|||";
    filelocaltxt.WriteLine(cadena);                    
}
    
answered by 21.04.2017 в 08:55