Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Comparing array of strings

Comparing array of strings

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netdata-structuresperformance
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    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;
        }
    
    M 1 Reply Last reply
    0
    • I indian143

      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;
          }
      
      M Offline
      M Offline
      Morgs Morgan
      wrote on last edited by
      #2

      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 path

      foreach ( 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

      I 1 Reply Last reply
      0
      • M Morgs Morgan

        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 path

        foreach ( 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

        I Offline
        I Offline
        indian143
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • I indian143

          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

          M Offline
          M Offline
          Morgs Morgan
          wrote on last edited by
          #4

          Yes, you can use List if you want. The ArrayList comes from the: using System.Collections;

          I 1 Reply Last reply
          0
          • M Morgs Morgan

            Yes, you can use List if you want. The ArrayList comes from the: using System.Collections;

            I Offline
            I Offline
            indian143
            wrote on last edited by
            #5

            Hi, Is there any way to get the which rows are repeated, like for example row 1, 3, 5 are same and 2 and 4 are same. Is there any way to get that from this list? thanks, Abdul.

            Thanks & Regards, Abdul Aleem Mohammad St Louis MO - USA

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups