score:0
Linq probably isn't your best bet here. Just try doing
var lines = File.ReadAllLines(filename);
List<string> linesICareABout = new List<string>();
for(int i = 0; !linesICareAbout[i].Contains("part2=002"); ++i)
{
linesICareABout.Add(lines[i]);
}
Then do whatever you want with the lines you read in.
However, if you're really dedicated to using Linq, try TakeWhile
score:1
The simplest and streghtforward solution comes to me is something like this:
var lines = File.ReadAllLines(@"C:\Sample.txt").
SkipWhile(line=>!line.Contains("part1")).
Skip(1).TakeWhile(line=>!line.Contains("part2"));
It returns result you want actually. The logic is simple:
SkipWhile
lines till meeting a line that contains "part1"- after
Skip(1)
(as it will be actually that one that contains "part1" string) - finally
Take
those ones till getting to the line containing "part2".
score:6
public static IEnumerable<string> GetLinesBetween(
string path,
string fromInclusive,
string toExclusive)
{
return File.ReadLines(path)
.SkipWhile(line => line != fromInclusive)
.TakeWhile(line => line != toExclusive);
}
var path = Path.Combine(sExecPath, sFileName); // don't combine paths like that
var result = GetLinesBetween(path, "part1=001", "part2=002").ToList();
score:8
I think you are looking for TakeWhile:
var linesInPartOne = File
.ReadAllLines(sExecPath + @"\" + sFileName)
.SkipWhile(line => !line.StartsWith("**part1="))
// To skip to part 1 header line, uncomment the line below:
// Skip(1)
.TakeWhile(line => !line.StartsWith("**part2="));
To generalize this to retrieve any given numbered part, something like this would do:
public static IEnumerable<String> ReadHeaderPart(String filePath, int part) {
return File
.ReadAllLines(filePath)
.SkipWhile(line => !line.StartsWith("**part" + part + "="))
// To skip to part 1 header line, uncomment the line below:
// Skip(1)
.TakeWhile(line =>
!line.StartsWith("**part" + (part + 1) + "="
&&
!line.StartsWith("end_header")))
.ToList();
}
EDIT: I had a Skip(1) in there to skip the part 1 header. Removed it since you seem to want to keep that line.
Source: stackoverflow.com
Related Query
- using LINQ to process a text file
- Read text data from file using LINQ
- C# .Net 3.5 Code to replace a file extension using LINQ
- Parsing a text File using Linq
- Using LINQ to query a text file
- reversing text from input file using linq
- Read text file word-by-word using LINQ
- Take unique records from text file using LINQ
- Linq - using not "IN" operator to select non duplicated lines from a text file
- Parsing a text file to CSV using LINQ
- I need to save the XML file using linq with xml code in C#
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- Using LINQ to get a string from a tab delimited text file into an array in the way I want
- How do I get SOAP xml file data using LINQ and display it in a form's text box? C#
- How to sort text file by Column with LINQ using start and end column positions
- How to use linq to process and conditionally read text file into a List<class> or a dictionary
- Read a text file using LinQ and then insert it into SQL Table using LinQ To SQL
- How to load xml code block into existing xml file at a specific node using linq
- Convert string[] to int[] in one line of code using LINQ
- How do I find the text within a div in the source of a web page using C#
- add data to existing xml file using linq
- Using text SQL with LINQ
- How can I sort a string of text followed by a number using LINQ
- read xml file using linq
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to read File names recursively from subfolder using LINQ
- How to read XML file using System.IO.Stream with LINQ
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
More Query from same tag
- Entity framework insert update problem!
- Observable Collections and Linq for Silverlight on Windows Phone
- How to allow null for foreign key to avoid NullReferenceException?
- How to group a period of time into yearly periods ? (split timespan into yearly periods)
- Linq to entities parametrized constructor Datetime
- How find similar records on some conditions in SQL or Linq or Entity Framework
- Lambda - user id in list existing in a list
- How to populate List<T> inside .Select() using LINQ
- filter the List and also take an extra element
- Checking for null returned items
- comparing two folders for non identical files with SymmetricDifference
- searching for an unnamed nested XElement by a specific XAttribute
- How can I display an image in my query using LINQ?
- DataContext.ExecuteQuery parameter issues
- Is it possible to use an IEnumerable as a private class-level variable?
- Alternatives of Datatable
- Emulating a join which uses a contains operator opposed to equals?
- Query items inside collection inside collection
- Get count of unique characters between first and last letter
- Reproduce a "DELETE NOT IN" SQL Statement via LINQ/Subsonic
- LINQ Join with Group By
- Set up self referencing class
- PredicateBuilder returns all users predicate.Or
- Can this SQL be done in LINQ?
- Rewriting a statement using LINQ(C#)
- How to Group By Distinct in EF
- How to use Expression for Linq union and intersect?
- Is there a conflict when using Data Caching and Lazy loading?
- updating a list using linq C#
- How to convert nested sql statement to linq to entities?