在C#中处理大的文本文件 [英] Processing large text file in C#

查看:312
本文介绍了在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 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.

这篇关于在C#中处理大的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆