I want to know if it is possible to read from a text file in a faster and smarter way.
This is a typical format of my data in a text file:
Call this “part”:
ID:1;
FIELD1 :someText;
FIELD2 :someText;
FIELD3 :someText;
FIELD4 :someText;
FIELD5 :someText;
FIELD6 :someText;
FIELD7 :someText;
FIELD8 :someText;
END_ID :
01: someData;
02: someData;
...
...
48: someData;
ENDCARD:
I have thousands of them in a text file.
Is it possible to use LINQ to read it “part” by “part”? I don’t want to loop through every single line.
Will it be possible for LINQ to start at ID:1; and end at ENDCARD:?
The reason for this is that i want to create a object for every “part”…
I had something like this in mind:
string[] lines = System.IO.File.ReadAllLines(SomeFilePath);
//Cleaning up the text file of unwanted text
var cleanedUpLines = from line in lines
where !line.StartsWith("FIELD1")
&& !line.StartsWith("FIELD5")
&& !line.StartsWith("FIELD8")
select line.Split(':');
//Here i want to LINQtoText "part" by "part"
//This i do not want to do!!!
foreach (string[] line in cleanedUpLines)
{
}
Here you go:
What this does is: create an iterator block (a state machine that reads and “yields” data as it is iterated; it does not read the entire file in one go) that scans the lines; if it is the end of a card, the card is “yielded”; otherwise it adds the data into a dictionary for storage.
Note: if you have your own
classthat represents the data, then you could use something like reflection or FastMember to set the values by name.This does not use LINQ directly; however, it is implemented as an enumerable sequence, which is the building block of LINQ-to-Objects, so you could consume this with LINQ, i.e.