在C#中处理大的文本文件文本文件

由网友(秒殺你的高傲)分享简介:我有4GB +的文本文件(CSV格式),我想在C#中使用LINQ来处理该文件。I have 4GB+ text files (csv format) and I want to process this file using linq in c#.我载CSV后运行复杂的LINQ查询,并转换为类?I run com...

我有4GB +的文本文件(CSV格式),我想在C#中使用LINQ来处理该文件。

I have 4GB+ text files (csv format) and I want to process this file using linq in c#.

我载CSV后运行复杂的LINQ查询,并转换为类?

I run complex linq query after load csv and convert to class?

但文件大小为4GB,虽然文件的应用程序的内存两倍大小。

but file size is 4gb although application memory double size of file.

我要如何处理(LINQ和新的结果)的大文件?

how can i process (linq and new result) large files?

感谢

推荐答案

不必加载整个文件到内存中,你可以读取和处理文件中的行由行。

Instead of loading whole file into memory, you could read and process the file line-by-line.

using (var streamReader = new StreamReader(fileName))
{
    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
        // analize line here
        // throw it away if it does not match
    }
}

如果您需要运行对文件中的数据进行复杂的查询,正确的做法是将数据加载到数据库中,并让DBMS照顾数据检索和存储管理。

If you need to run complex queries against the data in the file, the right thing to do is to load the data to database and let DBMS to take care of data retrieval and memory management.

阅读全文

相关推荐

最新文章