How to Read CSV file using LINQ to CSVProvider in C#

No.of Views3271
Bookmarked1 times
Downloads 
Votes0
By  RRaveen   On  01 Jan 2011 03:01:31
Tag : LINQ , General
In this article we will focus, how to read CSV file using LINQ to CSVProvider.I hope we know read CSV file using StreamReader or any other readers in C#. But when we are use the LINQ to CSVProvider, and then we could get additional features to query data in LINQ as like Sql Query.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article we will focus, how to read CSV file using LINQ to CSVProvider.I hope we know read CSV file using StreamReader or any other readers in C#. But when we are use the LINQ to CSVProvider, and then we could get additional features to query data in LINQ as like Sql Query.

Implementation

Create CSV File

Let’s create a simple command delimited csv file with few rows     and columns, the file like be below,

Image Loading

Create Demo Application

Open Visual studio and create project called as LINQToCSV.

Image Loading

Add a new class call CSVRow.cs for support to LINQ.

CSVRow.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQToCSV
{
    public class CSVRow
    {
        List<object> columns;

        public CSVRow()
        {
            columns = new List<object>();
        }

        internal void AddColumn(object value)
        {
            columns.Add(value);
        }

        public object this[int index]
        {
            get { return columns[index]; }
        }

        public string GetString(int index)
        {
            if (columns[index] is DBNull)
            {
                return null;
            }
            return columns[index].ToString();
        }

        public int Count
        {
            get { return this.columns.Count; }
        }
    }
}

The CSVRow is supportive class for CSV Provider class. Within this class we have list object keep the rows and also few useful methods to query data using LINQ.


Then another a new class to implement CSVProvider for support LINQ Query operations. New class name is LINQToCSVProvider.cs.

LINQToCSVProvider.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace LINQToCSV
{
    public class LINQToCSVProvider : IEnumerable<CSVRow>
    {

        private string delimiter;
        private string filePath;
        private List<CSVRow> rows;

        public LINQToCSVProvider()
        {
            rows = new List<CSVRow>();
        }
        public static LINQToCSVProvider Create(string filePath, string delimiter)
        {
            LINQToCSVProvider csvProvider = new LINQToCSVProvider();
            csvProvider.filePath = filePath;
            csvProvider.delimiter = delimiter;
            return csvProvider;
        }
        private void BuildRows()
        {
            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Text;HDR=YES;FMT={1}""";
            connectionString = string.Format(connectionString, System.IO.Path.GetDirectoryName(filePath), delimiter);
            rows.Clear();
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM " + System.IO.Path.GetFileName(filePath), conn))
                {

                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            CSVRow newRow = new CSVRow();
                            for (int count = 0; count < reader.FieldCount; count++)
                            {
                                newRow.AddColumn(reader[count]);
                            }
                            rows.Add(newRow);
                        }
                    }
                }
            }
        }

        #region IEnumerable<CSVRow> Members

        public IEnumerator<CSVRow> GetEnumerator()
        {
            BuildRows();
            return rows.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            BuildRows();
            return rows.GetEnumerator();
        }

        #endregion
    }
}

The LINQToCSVProvider is important class to use the LINQ query operation existing records within this object. This class is inherited from IEnumerable generic interface with CSVRow. The LINQToCSVProvider has two methods as well,

1.    Create()

This Method is creating object of the provider class with file name and what is delimiter to use in later part of the code to process query operation.

2.    BuildRows()

This method help to read data from source and add each row in list object to perform querying operation as like shown later part of this article.

And finally implementation of the IEnumerable interface methods. I assume you all know how to read CSV file with OleDbConnection.

How to use LINQToCSVProvider?

C# Code   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQToCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            LINQToCSVProvider provider = LINQToCSVProvider.Create(@".........\source.csv", ",");
            foreach (CSVRow row in (from source in provider select source))
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", row.GetString(0), row.GetString(1), row.GetString(2), row.GetString(3), row.GetString(4));
            }
        }
    }
}

Code Explanation

1.    In order query data using LINQ, first we have to create CSVProvider object with source file and delimiter. The first argument is source file path and second argument is what is delimited for the CSV source file to spilt values.

LINQToCSVProvider provider = LINQToCSVProvider.Create(@"....\source.csv", ",");

2.    In next foreach block, I’m query from CSVProvider object and then assign to CSVRow.

foreach (CSVRow row in (from source in provider select source))
    {
	// print records
    }

3.    Within looping print records from CSVRow object as formatted string

Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", row.GetString(0), row.GetString(1), row.GetString(2), row.GetString(3), row.GetString(4));

Now run application and see output as like below,

Output 

Image Loading

Apart from this default querying you can select records for following caes as well,

1.    Select records for exactly match word or character

var data =from source in provider where source.GetString(1) == "Wrox" select source;

2.    Select records for start with word or character

var data = from source in provider where source.GetString(1).StartsWith("Wr") select source

3.    Select records end with word or character

var data= from source in provider where source.GetString(1).EndsWith("ss") select source

4.    Select records for wherever match word or character
 

var data= from source in provider where source.GetString(1).Contains("s") select source

 Note: Just only change the querying way, other data processing still remaining as it.

Download Sample Project

Download source files -32 kb

Conclusion

In this article, we have learned how to read CSV file using LINQ query and manipulate data in easy way. Support different ways for querying data with LINQToCSVProvider. Hope help and thank you for reading.
 

 

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type -Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
    Deferred Execution executes the query only when Loop starts. What I mean here is that, we iterate through the query variable to get the result.
    Published Date : 15/Jan/2011
    In this article we will focus LINQ Remote and Local exaction such as This query executes on the server, Remote execution of query is default in LINQ, In Remote execution advantage of Databases index can be taken, Remote execution allows us to take the advantage of Database server engine, Remote execution allows us to only select particular rows from the table. This is very useful when we do have large amount of data in the server.
    Published Date : 07/Jan/2011
    In this article, we will see how to work with LINQ against IIS.
    Published Date : 10/Aug/2010
    how to create a XML tree using Functional Construction method of LINQ to XML.
    Published Date : 13/Apr/2010
    This article demonstrates how to use LINQ on DataTable, XML Data using LINQ to XML and SQL server data base using LINQ to SQL Classes.
    Published Date : 28/Jun/2010
Comments
By:RRaveenDate Of Posted:1/18/2011 9:12:53 AM
Thank you
Hi, thank you for your feedback. we will publish more soon.
By:oQDate Of Posted:1/17/2011 12:40:21 PM
Great article
Great definitely going to try this out.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top