here is a neat piece of code in C# LINQ, to read numbers from a text file:
sample text file:
#comment lines look like this
#
#here is my list of numbers:
100
101
102
credit: this is based on the excellent book LINQ to Objects Using C# 4.0 by Troy Magennis
private List<int> getNumbersFromFile(string filePath)
{
var lines = File.ReadAllLines(filePath);
var licIds =
lines.SkipWhile(li => li.StartsWith("#"))
.TakeWhile(li => !string.IsNullOrEmpty(li.Trim()))
.Where(li
=> !li.StartsWith("#"))
.Select(li => int.Parse(li)) ; return licIds.ToList(); }
sample text file:
#comment lines look like this
#
#here is my list of numbers:
100
101
102
credit: this is based on the excellent book LINQ to Objects Using C# 4.0 by Troy Magennis
Comments
Post a Comment