简体   繁体   English

Linq查询结合两个或三个表达式

[英]Linq query combine two or three expressions

I am getting lineCount, boolean value and lastline from the text file with the below queries 我通过以下查询从文本文件中获取lineCount,布尔值和最后一行

 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.. 提前谢谢了..

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: 编辑:如果您正在寻找迭代 FileInfo数组的方法,我可以建议以下表达式:

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 . 尽管我建议您创建一些类来存储有关文件的所有信息,而不要使用Tuple

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();
 }

Store the result of File.ReadLines into a local variable and use that one File.ReadLines的结果存储到本地变量中并使用该变量

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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM