Linq 到文本文件 [英] Linq To Text Files

查看:27
本文介绍了Linq 到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件(抱歉,我不允许处理 XML 文件:(),它包含客户记录.每个文本文件看起来像:

I have a Text File (Sorry, I'm not allowed to work on XML files :(), and it includes customer records. Each text file looks like:

Account_ID: 98734BLAH9873
User Name: something_85
First Name: ILove
Last Name: XML
Age: 209

等等...而且我需要能够使用 LINQ 从这些文本文件中获取数据并将它们存储在内存中.

etc... And I need to be able to use LINQ to get the data from these text files and just store them in memory.

我见过很多 Linq to SQL、Linq to BLAH,但对 Linq to Text 一无所知.有人可以帮我一下吗?

I have seen many Linq to SQL, Linq to BLAH but nothing for Linq to Text. Can someone please help me out abit?

谢谢

推荐答案

你可以这样使用代码

var pairs = File.ReadAllLines("filename.txt")
    .Select(line => line.Split(':'))
    .ToDictionary(cells => cells[0].Trim(), cells => cells[1].Trim())

或使用 .NET 4.0 File.ReadLines() 方法返回一个 IEnumerable,这对于处理大文本文件很有用.

Or use the .NET 4.0 File.ReadLines() method to return an IEnumerable, which is useful for processing big text files.

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

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