Glory Raj
Glory Raj

Reputation: 17701

Linq query combine two or three expressions

I am getting lineCount, boolean value and lastline from the text file with the below queries

 private void ReadFile(FileInfo[] TXTFiles, out int totalLineCount, out bool result, out string footerContent)
 {
     totalLineCount = File.ReadLines(TXTFiles[0].FullName).Count();
     result = File.ReadLines(TXTFiles[0].FullName).Last().StartsWith(fileFooterIdentify);
     footerContent = File.ReadLines(TXTFiles[0].FullName).Last();
 }

here I am reading file for three times it will little bit hit the performance us there any possible to combine those three lines into single expression and looking to make single read action for file ..

Could any one help me on this how to make single expression among the above.

many thanks in advance..

Upvotes: 2

Views: 127

Answers (3)

Sir Rufo
Sir Rufo

Reputation: 19106

Store the result of File.ReadLines into a local variable and use that one

private void ReadFile(FileInfo[] TXTFiles, out int totalLineCount, out bool result, out string footerContent)
 {
     var content = File.ReadLines(TXTFiles[0].FullName);
     totalLineCount = content.Count();
     footerContent = content.Last();
     result = footerContent.StartsWith(fileFooterIdentify);
 }

That will read the file only two times.

To have only a single read action change to

private void ReadFile(FileInfo[] TXTFiles, out int totalLineCount, out bool result, out string footerContent)
{
    var content = File.ReadLines( TXTFiles[0].FullName );
    totalLineCount = 0;
    foreach ( var row in content )
    {
        totalLineCount++;
        footerContent = row;
    }
    result = footerContent.StartsWith( fileFooterIdentify );
}

Upvotes: 0

Dmitry Volkov
Dmitry Volkov

Reputation: 1337

Perhaps I didn't fully understand your requirements, but if it is the performance that worries you, I would suggest the following:

private void ReadFile(FileInfo[] TXTFiles, out int totalLineCount, out bool result, out string footerContent)
 {
     var fileContents = File.ReadLines(TXTFiles[0].FullName);
     totalLineCount = fileContents.Count();
     result = fileContents.Last().StartsWith(fileFooterIdentify);
     footerContent = fileContents.Last();
 }

From what I know, transforming it into a single expression will neither additionally improve your performance, nor will make it more readable.

EDIT: If you were looking for the way to iterate over your FileInfo array, I can suggest the following expression:

var fileReadInfo = TXTFiles.Select(file =>
{
    var fileContents = File.ReadAllLines(file.FullName);
    return new Tuple<int, bool, string>(fileContents.Count(),
        fileContents.Last().StartsWith(fileFooterIdentify), fileContents.Last());
});

Though I would recommend you to create some class to store all information about file, instead of using Tuple.

Upvotes: 6

Romano Zumb&#233;
Romano Zumb&#233;

Reputation: 8099

Just save the result of the file read as an array:

private void ReadFile(FileInfo[] TXTFiles, out int totalLineCount, out bool result, out string footerContent)
 {
     string[] lines = File.ReadLines(TXTFiles[0].FullName);
     totalLineCount = lines.Count();
     result = lines.Last().StartsWith(fileFooterIdentify);
     footerContent = lines.Last();
 }

Upvotes: 0

Related Questions