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:
string delimiter = ",;";
List<string> logs = (File.ReadAllLines(@"C:\Data.txt")
.Where(line => !string.IsNullOrEmpty(line))
.Select(line => line.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
.Where(values => values[2].Equals("CityA", StringComparison.CurrentCultureIgnoreCase))
.Where(values => int.Parse(values[1]) >= 30)
.Select(values => string.Join("_", values))
.Distinct()
.ToList<string>());
Hope this will help you out in reading delimited files :)
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.