1

I've got a problem. I need to write a csv file (whit append) but i can't write the item that are duplicate in the file. So:

I read all item in the csv file, I add the item to my list (that then I write to the file), and I want to remove duplicate from the list before writing in the file... I try to use "distinct" but it doesn't work.

My class:

class MyList_Base
{
  public string Cliente  {get; set; }
  public double   TotaleProvvigione  { get; set; }
  public double   TotalePunti        { get; set; }
  public string   Descrizione        { get; set; }
  public double   Pezzi              { get; set; }
  public double   Provvigione        { get; set; } 
  public bool     DigitalPen         { get; set; }
  public bool     ReferenzaPersonale { get; set; }
  public DateTime Data               { get; set; }
  public string   Mese               { get; set; }
  public string   Gara               { get; set; }
  public double   PuntiGara          { get; set; }
  public string   Note               { get; set; }


  public MyList_Base(string cliente, double totaleProvvigione, double totalePunti, string descrizione, double pezzi, double provvigione, bool digitalPen, bool referenzaPersonale, DateTime data, string mese, string gara, double puntiGara, string note)
  {
    Cliente            = cliente;
    TotaleProvvigione  = totaleProvvigione;
    TotalePunti        = totalePunti;
    Descrizione        = descrizione;
    Pezzi              = pezzi;
    Provvigione        = provvigione;
    DigitalPen         = digitalPen;
    ReferenzaPersonale = referenzaPersonale;
    Data               = data;
    Mese               = mese;
    Gara               = gara;
    PuntiGara          = puntiGara;
    Note               = Note;
  }

  public override string ToString()
  {
    return Cliente + ";" + TotaleProvvigione + ";" + TotalePunti + ";" + Descrizione + ";" + Pezzi + ";" + Provvigione + ";" + DigitalPen + ";" + ReferenzaPersonale + ";" + Data + ";" + Mese + ";" + Gara + ";" + PuntiGara+";" + Note;
  }

  public static MyList_Base Parse(string csv)
  {
    string[] tmp = csv.Split(';');
    return new MyList_Base(tmp[0], Convert.ToDouble(tmp[1]), Convert.ToDouble(tmp[2]), tmp[3], Convert.ToDouble(tmp[4]), Convert.ToDouble(tmp[5]), Convert.ToBoolean(tmp[6]), Convert.ToBoolean(tmp[7]), Convert.ToDateTime(tmp[8]), tmp[9], tmp[10], Convert.ToDouble(tmp[11]), tmp[11]);
  }

}

And this is what I need to do : "//TODO: REMOVE DUPLICATE"

public List<MyList_Base> LoadMyData(string nomeFile)
{
  if (!File.Exists(nomeFile)) return null;

  List<MyList_Base> tmp = new List<MyList_Base>();
  StreamReader sr = new StreamReader(nomeFile);

  while(!sr.EndOfStream)
  {
    tmp.Add(MyList_Base.Parse(sr.ReadLine()));
  }

  sr.Close();
  return tmp;
}

public bool Save(string nomeFile)
{
  var tmp = LoadMyData(nomeFile);
  StreamWriter sw = new StreamWriter(nomeFile);

  //TODO: REMOVE DUPLICATE 
  foreach (var x in this)
  {
     sw.WriteLine(x.ToString());
  }

  sw.Close();
  return true;
}

Can someone help me?

PS: sorry for my bad english.. Fabio.

S.Spieker
  • 7,005
  • 8
  • 44
  • 50
Fabio
  • 593
  • 3
  • 14

2 Answers2

3

Enumerable.Distinct extension method may help you.

There is an overload with an IEqualityComparer if you can't use the default (Equals method of MyList_Base). This may be the case if e.g. your Equals method would only compare some of the fields, but you would want to check all them.

molnargab
  • 162
  • 7
0

You can use Distinct extension method of the list and pass an object which implements IEqualityComparer. Check this:

http://blog.alex-turok.com/2013/03/c-linq-and-iequalitycomparer.html

Randeep Singh
  • 1,275
  • 14
  • 29