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.