LINQ到文本文件文本文件、LINQ

2023-09-02 20:47:40 作者:湮灭°

我有一个文本文件(对不起,我不能工作在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到SQL,LINQ到BLAH,但没有对LINQ到文本。是否有人可以帮助我升技?

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

感谢您

推荐答案

您可以用code一样,

You can use the code like that

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.