J'ai trois System.Array où je stocke essentiellement des numéros de téléphone avec 10 caractères. La définition est la suivante:Rechercher si un élément est présent dans System.Array ou non et s'il existe supprimer de l'original
private static System.Array objRowAValues;
private static System.Array objRowBValues;
private static System.Array objRowCValues;
Je le fais parce que je lis un énorme fichier Excel avec de nombreuses cellules (environ 1 000 000) et le processus avec List<string>
est un peu lent. Plus tard dans mon code je change le System.Array à un List<string>
principalement parce que je remplis un éléments ListBox. Ce sont comment puis-je faire la définition de la liste
private List<string> bd = new List<string>();
private List<string> bl = new List<string>();
private List<string> cm = new List<string>();
Je veux vérifier si toutes les valeurs de objRowAValues existe dans objRowBValues ou objRowCValues et si EXISTE supprimer la valeur existante, cela? Je suis newbiew avec C# et c'est mes premiers pas.
EDIT: voici le code (parties pertinentes uniquement) de ce que je fais:
private List<string> bd = new List<string>();
private static System.Array objRowAValues;
private List<string> bl = new List<string>();
private static System.Array objRowBValues;
private List<string> cm = new List<string>();
private static System.Array objRowCValues;
private List<string> pl = new List<string>();
private static Microsoft.Office.Interop.Excel.Application appExcel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range rngARowLast, rngBRowLast, rngCRowLast;
long lastACell, lastBCell, lastCCell, fullRow;
// this is the main method I use to load all three Excel files and fill System.Array and then convert to List<string>
private void btnCargarExcel_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (System.IO.File.Exists(openFileDialog1.FileName))
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
filePath.Text = openFileDialog1.FileName.ToString();
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(openFileDialog1.FileName, 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false,
false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
fullRow = xlWorkSheet.Rows.Count;
lastACell = xlWorkSheet.Cells[fullRow, 1].End(Excel.XlDirection.xlUp).Row;
rngARowLast = xlWorkSheet.get_Range("A1", "A" + lastACell);
objRowAValues = (System.Array) rngARowLast.Cells.Value;
foreach (object elem in objRowAValues) { bd.Add(cleanString(elem.ToString(), 10)); }
nrosProcesados.Text = bd.Count().ToString();
listBox1.DataSource = bd;
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
executiontime.Text =
String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10).ToString();
}
else
{
MessageBox.Show("No se pudo abrir el fichero!");
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
appExcel = null;
System.Windows.Forms.Application.Exit();
}
}
}
// This is the method where I clean the existent values from bd (the List where I should remove existent values on bl and existent values on cm
private void btnClean_Click(object sender, EventArgs e)
{
pl = bd;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
pl.RemoveAll(element => bl.Contains((element)));
pl.RemoveAll(element => cm.Contains((element)));
textBox2.Text = pl.Count.ToString();
listBox4.DataSource = pl;
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
textBox6.Text =
String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10).ToString();
}
Si vous avez besoin de collections de tailles variables, n'utilisez pas de tableaux. Utilisez des listes génériques - ['List'] (http://msdn.microsoft.com/fr-fr/library/6sh2ey19.aspx), par exemple. Je suggère également de lire sur [LINQ] (http://msdn.microsoft.com/en-gb/library/vstudio/bb397926.aspx). –
Oded
@Oded J'ai édité le post, voir si c'est ce que vous référez sur votre commentaire et que puis-je faire avec LINQ? – Reynier
Eh bien, c'est un début. Quant à LINQ - pourquoi ne pas lire l'introduction sur le lien que j'ai posté? – Oded