Wednesday, April 4, 2012

LINQ - Reading a csv file using LINQ

reading a csv file using LINQ.

Sample Data
I will use a sample file which contains a data about customers. When working with text files we must know the number of columns and the data contained in each column. Below is a list of columns in their right order for our file.
1.      First Name
2.      Last Name
3.      Job Title
4.      City
5.      Country
The file itself will contain this data. I have pulled this out of Employees table in Northwind database.

First Name  Last Name  Job Title  City  Country
Sharan       Raj              SE           Bang India

Reading Data
Before we start reading our csv file we will create a class which will hold a record we will read from our csv file. For this I will create a customer class which looks like this.


public class Customer
{
   string Firstname { get; set; }
   string Lastname { get; set; }
   string JobTitle { get; set; }
   string City { get; set; }
   string Country { get; set; }
}


Reading Entire File
Now we are ready to read data from our file using LINQ. Using this code we can read the entire file. I am also using a foreach statement to output the results.


var query =
       from line in File.ReadAllLines(filePath)
       let customerRecord = line.Split(',')
       select new Customer()
           {
               Firstname = customerRecord[0],
               Lastname = customerRecord[1],
               JobTitle = customerRecord[2],
               City = customerRecord[3],
               Country = customerRecord[4]
           };
foreach (var item in query)
{
   Console.WriteLine("{0}, {1}, {2}, {3}, {4}",
       item.Firstname, item.Lastname, item.JobTitle, item.City, item.Country);
}


File.ReadAllLines() returns an array of lines and we then use the split function of array to split it by a comma. Its just that simple.
Reading selected records
We can use this code to read all customers who live in UK.


var query =
       from c in
           (from line in File.ReadAllLines(filePath)
            let customerRecord = line.Split(',')
            select new Customer()
                {
                    Firstname = customerRecord[0],
                    Lastname = customerRecord[1],
                    JobTitle = customerRecord[2],
                    City = customerRecord[3],
                    Country = customerRecord[4]
                })
       where c.Country == "UK"
       select c;


This code can be used to read customers who have sales in their job title.


var query =
       from c in
           (from line in File.ReadAllLines(filePath)
            let customerRecord = line.Split(',')
            select new Customer()
                {
                    Firstname = customerRecord[0],
                    Lastname = customerRecord[1],
                    JobTitle = customerRecord[2],
                    City = customerRecord[3],
                    Country = customerRecord[4]
                })
       where c.JobTitle.Contains("Sales")
       select c;

No comments:

Post a Comment