Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using LINQ to read delimited text files

0.00/5 (No votes)
28 Mar 2010 1  
Did you ever face a situation where you want to read a delimited file (CSV or any similar format) and want to filter the records on the basis of some conditions (by checking some values against columns).For example, let's assume that Data.txt file contains the following...
Did you ever face a situation where you want to read a delimited file (CSV or any similar format) and want to filter the records on the basis of some conditions (by checking some values against columns).

For example, let's assume that Data.txt file contains the following records:

Name,Age,City
Person1,30,CityA
Person2,20,CityB
Person3,25,CityB
Person4,30,CityA
Person5,27,CityA


If we want to find all records which belong to CityA with age >= 30 then we can use LINQ for this purpose:

C#
string delimiter = ",;";
List<string> logs = (File.ReadAllLines(@"C:\Data.txt")
    // leave blank lines
    .Where(line => !string.IsNullOrEmpty(line))
    // split line seperated by delimiter
    .Select(line => line.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
    // compare the third column to find all records from CityA
    .Where(values => values[2].Equals("CityA", StringComparison.CurrentCultureIgnoreCase))
    // compare the second column to find all records with age more than or equal to 30
    .Where(values => int.Parse(values[1]) >= 30)
    // join back the splitted values by underscore
    .Select(values => string.Join("_", values))
    // find all unique values
    .Distinct()
    .ToList<string>());// convert to list


Hope this will help you out in reading delimited files :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here