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

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

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

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