Click here to Skip to main content
15,867,308 members
Articles / General Programming / File
Tip/Trick

Using LINQ to read delimited text files

Rate me:
Please Sign up or sign in to vote.
4.90/5 (26 votes)
28 Mar 2010CPOL 70.5K   25   17
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India

Manoj Kumar is a Humble Programmer and a trainer having dozens of trainings, publications and articles to his wallet.


His programming adventures began with Basic at an age of 11. Being a mathematician at core, soon he started looking for some more and moved to assembly language, later to C, C++, VC++ and finally to .Net.


He started his professional career as a VC++ 6 trainer, moved to embedded systems and device driver development, then to complex biological systems and finally moved to pure application development.


He has been teaching and training people for more than 12 years on a wide range of topics including Mathematics, Algorithms, Data Structures, C, C++, VC++, MFC, C#, Design Patterns and now a days he is working extensively with Visual Studio and .Net framework which includes VSX, WPF, Silverlight, WCF, WF, XAML and RIAs.


Awards:


  • Ideablade RIA Service Challenge winner
  • Visual Studio 2010 Extension Contest winner (Best Use of Editor)


Visit my website and blog or drop me a mail.


Feel free to connect with me on Linkedin.


Comments and Discussions

 
QuestionVery smart solution!!! Pin
ATeDe26-Apr-18 5:15
ATeDe26-Apr-18 5:15 
GeneralMy vote of 5 Pin
nocturns229-Sep-13 12:16
nocturns229-Sep-13 12:16 
GeneralRe: My vote of 5 Pin
The Manoj Kumar15-Oct-13 17:29
The Manoj Kumar15-Oct-13 17:29 
GeneralRe: My vote of 5 Pin
nocturns216-Oct-13 11:10
nocturns216-Oct-13 11:10 
QuestionLine word‏ Pin
erummirza30-Jul-13 17:24
erummirza30-Jul-13 17:24 
QuestionLine word Pin
erummirza29-Jul-13 23:24
erummirza29-Jul-13 23:24 
AnswerRe: Line word Pin
The Manoj Kumar30-Jul-13 6:28
The Manoj Kumar30-Jul-13 6:28 
GeneralRe: Line word Pin
erummirza10-Aug-13 0:55
erummirza10-Aug-13 0:55 
GeneralMy vote of 5 Pin
Carsten V2.012-Aug-12 4:50
Carsten V2.012-Aug-12 4:50 
GeneralRe: My vote of 5 Pin
The Manoj Kumar1-Dec-12 20:12
The Manoj Kumar1-Dec-12 20:12 
GeneralRe: My vote of 5 Pin
Carsten V2.01-Dec-12 21:21
Carsten V2.01-Dec-12 21:21 
GeneralReason for my vote of 5 good Pin
vijay_dahite5-Aug-10 3:17
vijay_dahite5-Aug-10 3:17 
GeneralRe: Reason for my vote of 5good Pin
The Manoj Kumar1-Dec-12 20:12
The Manoj Kumar1-Dec-12 20:12 
GeneralReason for my vote of 5 good job~ Pin
puyunhong14-Jul-10 21:13
puyunhong14-Jul-10 21:13 
GeneralRe: Reason for my vote of 5good job~ Pin
The Manoj Kumar1-Dec-12 20:12
The Manoj Kumar1-Dec-12 20:12 
GeneralNicely done! Pin
Roger50015-Apr-10 13:52
Roger50015-Apr-10 13:52 
GeneralRe: Nicely done! Pin
The Manoj Kumar16-Apr-10 5:02
The Manoj Kumar16-Apr-10 5:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.