Comparing array of strings
-
Hi, I have an asp.net application which loads data from csv file. I need to check for duplicate rows in the csv file and should not process them and give error message for those duplicate rows. I did in some approach but got the comments like performance is impacted. Is there any performance oriented process available to compare the string arrays Any type of help is greatly appreciate. Here is my code
private static bool CheckForDuplicateRow(FileInfo file
,string[] line
,int lineNumber
,ref bool lineAlreadyVerified)
{
CsvStream csvStream;using (Stream fileStream = file.OpenRead()) { using (StreamReader reader = new StreamReader(fileStream)) { csvStream = new CsvStream(reader); string\[\] rowItems; try { csvStream.GetNextRow(); //This is for header values, we are not comparing for header values rowItems = csvStream.GetNextRow(); int rownumber = 1; while (rowItems != null) { if ((rowItems.Length == line.Length) && (rownumber != lineNumber) && (rowItems.Length > 0)) { bool b = AreRowItemsEqual(rowItems, line); if (AreRowItemsEqual(rowItems, line) == true) { if (lineNumber < rownumber) lineAlreadyVerified = true; return true; } } rowItems = csvStream.GetNextRow(); rownumber++; } } catch (Exception e) { throw e; } } } return false; } private static bool AreRowItemsEqual(string\[\] rowItems, string\[\] line) { for (int i = 0; i < rowItems.Length; i++) { if (rowItems\[i\] != line\[i\]) { return false; } } return true; }
-
Hi, I have an asp.net application which loads data from csv file. I need to check for duplicate rows in the csv file and should not process them and give error message for those duplicate rows. I did in some approach but got the comments like performance is impacted. Is there any performance oriented process available to compare the string arrays Any type of help is greatly appreciate. Here is my code
private static bool CheckForDuplicateRow(FileInfo file
,string[] line
,int lineNumber
,ref bool lineAlreadyVerified)
{
CsvStream csvStream;using (Stream fileStream = file.OpenRead()) { using (StreamReader reader = new StreamReader(fileStream)) { csvStream = new CsvStream(reader); string\[\] rowItems; try { csvStream.GetNextRow(); //This is for header values, we are not comparing for header values rowItems = csvStream.GetNextRow(); int rownumber = 1; while (rowItems != null) { if ((rowItems.Length == line.Length) && (rownumber != lineNumber) && (rowItems.Length > 0)) { bool b = AreRowItemsEqual(rowItems, line); if (AreRowItemsEqual(rowItems, line) == true) { if (lineNumber < rownumber) lineAlreadyVerified = true; return true; } } rowItems = csvStream.GetNextRow(); rownumber++; } } catch (Exception e) { throw e; } } } return false; } private static bool AreRowItemsEqual(string\[\] rowItems, string\[\] line) { for (int i = 0; i < rowItems.Length; i++) { if (rowItems\[i\] != line\[i\]) { return false; } } return true; }
indian143 wrote:
I have an asp.net application which loads data from csv file. I need to check for duplicate rows in the csv file and should not process them and give error message for those duplicate rows.
Here's my approach:
//list to add lines in your csv file
ArrayList uniqueLines = new ArrayList();
ArrayList duplicateLines = new ArrayList();//Read all lines from your csv file
String[] lines = File.ReadAllLines( "C:/file.csv" );//passing the file pathforeach ( String line in lines )
{
if ( !uniqueLines.Contains( line ) )
{
uniqueLines.Add( line );
}
else
{
duplicateLines.Add( line );
}}
From the above, you have all the unique lines in the uniqueLines arraylist and you have duplicate lines in the duplicateLines arraylist. To display errors, loop through the "duplicateLines" and tell the user about the error. Happy Coding, Morgs
-
indian143 wrote:
I have an asp.net application which loads data from csv file. I need to check for duplicate rows in the csv file and should not process them and give error message for those duplicate rows.
Here's my approach:
//list to add lines in your csv file
ArrayList uniqueLines = new ArrayList();
ArrayList duplicateLines = new ArrayList();//Read all lines from your csv file
String[] lines = File.ReadAllLines( "C:/file.csv" );//passing the file pathforeach ( String line in lines )
{
if ( !uniqueLines.Contains( line ) )
{
uniqueLines.Add( line );
}
else
{
duplicateLines.Add( line );
}}
From the above, you have all the unique lines in the uniqueLines arraylist and you have duplicate lines in the duplicateLines arraylist. To display errors, loop through the "duplicateLines" and tell the user about the error. Happy Coding, Morgs
-
Can I use List instead of ArrayList, because that class is not coming in my application, I dont know why?
Thanks & Regards, Abdul Aleem Mohammad St Louis MO - USA
Yes, you can use List if you want. The ArrayList comes from the: using System.Collections;
-
Yes, you can use List if you want. The ArrayList comes from the: using System.Collections;