|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWith the introduction of LINQ in C# 3.0, the life of a developer has become very easy when it comes to querying collections. In this article, I will show you a quick and easy way to query large CSV files. BackgroundImagine you have a very large CSV file. Loading the entire file into memory (e.g. Using the CodeI have implemented public class TextFileReader : IEnumerable<string[]>
{
private string _fileName = string.Empty;
public TextFileReader(string fileName)
{
this._fileName = fileName;
}
#region IEnumerable<string[]> Members
IEnumerator<string[]> IEnumerable<string[]>.GetEnumerator()
{
using (StreamReader streamReader = new StreamReader(this._fileName))
{
while (!streamReader.EndOfStream)
{
yield return streamReader.ReadLine().Split(new char[] { ',' });
}
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
IEnumerable<string[]> iEnumerable = this;
yield return iEnumerable.GetEnumerator();
}
#endregion
}
Using this class is very simple. Just initialize the class with the CSV file name and you are all set to LINQ it using standard operator. The following code shows how to query all the rows in the CSV file with the first column text starting with ' TextFileReader reader = new TextFileReader(@"Sample.txt");
var query = from it in reader
where it[0].StartsWith("a")
select new { Name = it[0], EmailAddress = it[1] };
foreach (var x in query)
{
Console.WriteLine(String.Format("Name={0} EmailAddress = {1}",
x.Name, x.EmailAddress));
}
Points of InterestWell, this may not be the best way to implement this. I am also into the process of learning LINQ. Please feel free to give your comments or suggestions. In my next article, I will show you how to write a custom query provider for CSV files. Please stay tuned. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||