Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
these are my two Input CSV files

https://drive.google.com/file/d/0BxsoIsUEjoORSTBSdDZOVF9YZ1k/edit?usp=sharing[^]

I have to compare these files build a new table and sort in smallest to largest values (values are -ve)
The values when sorted in smallest to largest get sorted in largest to smallest

In project Add Reference--> Microsoft.VisualBasic

C#
using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;


namespace ReadDataFromCSVFile
{
    static class Program
    {
        static void Main()
        {
            string csv_file_path = @"C:\Matlab\Limits.csv";
            DataTable table1 = GetDataTabletFromCSVFile(csv_file_path);


            string csv_file_path1 = @"C:\Matlab\Sheet1_t168h.csv";
            DataTable table2 = GetDataTabletFromCSVFile1(csv_file_path1);


            DataTable table3 = CompareTwoDataTable(table1,table2);


            Console.WriteLine("Rows count:" + table3.Rows[1]                 
                               ["CON_ALL:open_ADCI1_N_(ADCI1_N)@ADCI1_N[1]"].ToString());
            Console.ReadLine();

        }  
       private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable table1 = new DataTable("Limits");

            using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
            {
                csvReader.SetDelimiters(new string[] { "," });
                csvReader.HasFieldsEnclosedInQuotes = true;
                string[] colFields = csvReader.ReadFields();
                foreach (string column in colFields)
                {
                    DataColumn datecolumn1 = new DataColumn(column);
                    datecolumn1.AllowDBNull = true;
                    table1.Columns.Add(datecolumn1);
                }
                while (!csvReader.EndOfData)
                {
                    string[] fieldData = csvReader.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData.Length; i++)
                    {
                        if (fieldData[i] == "")
                        {
                            fieldData[i] = null;
                        }
                    }
                    table1.Rows.Add(fieldData);

                }
                DataRow[] rows1;
                rows1 = table1.Select("SKIP = '1'");
                foreach (DataRow r in rows1)
                    r.Delete();
            }

            return table1;
        }

        private static DataTable GetDataTabletFromCSVFile1(string csv_file_path1)
        {
            DataTable table2 = new DataTable("Real");

            using (TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1))
            {
                csvReader1.SetDelimiters(new string[] { "," });
                csvReader1.HasFieldsEnclosedInQuotes = true;
                string[] colFields = csvReader1.ReadFields();
                foreach (string column in colFields)
                {
                    DataColumn datecolumn2 = new DataColumn(column);
                    datecolumn2.AllowDBNull = true;
                    table2.Columns.Add(datecolumn2);
                }
                while (!csvReader1.EndOfData)
                {
                    string[] fieldData1 = csvReader1.ReadFields();
                    //Making empty value as null
                    for (int i = 0; i < fieldData1.Length; i++)
                    {
                        if (fieldData1[i] == "")
                        {
                            fieldData1[i] = null;
                        }
                    }
                    table2.Rows.Add(fieldData1);

                }

            }

            return table2;
        }

private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
        {
            DataTable table3 = new DataTable();
            DataRow dr = null;
  string filterExp = string.Empty;
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                 
                string col = table1.Rows[i]["Par Name"].ToString();
                if (table2.Columns.Contains(col))
                {
                    if (!table3.Columns.Contains(col))
                    {
                        table3.Columns.Add(col, typeof(String));
                         filterExp = filterExp+col+" asc ,";
                    }
 
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            if (table3.Rows.Count != table2.Rows.Count)
                            {
                                    dr = table3.NewRow();
                                    table3.Rows.Add(dr);
                            }
                            table3.Rows[j][col] = (table2.Rows[j][col]);
                        }
                   
                }
 
            } 
               DataView dv = new DataView(table3);
              filterExp =filterExp.TrimEnd(',');
               dv.Sort = filterExp;
                table3 = dv.ToTable();
 
            return table3;
           }
       
       }
   
    }
Posted
Updated 5-Mar-14 3:31am
v5
Comments
Richard MacCutchan 5-Mar-14 9:03am    
Please use <pre> tags around your code as I have done. Also in future, when asked to edit your question, use the Improve question link to edit the original, rather than opening a new thread.
FarhanShariff 5-Mar-14 9:03am    
Sure.Thank you for the edit :)
Richard MacCutchan 5-Mar-14 9:06am    
I'm not sure what all that is doing but you could simplify your data access considerably by reading http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom. And why do you have two methods (GetDataTable...) which do essentially the same job?
FarhanShariff 5-Mar-14 9:13am    
The files have different fields.Ill read the other import method
FarhanShariff 5-Mar-14 9:25am    
I read Microsoft.VisualBasic.FileIO.TextFieldParser dll has better performance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900