【发布时间】:2011-05-04 11:38:33
【问题描述】:
我是 C# 新手,所以需要你的帮助。
我有一个包含很多行和 3 个制表符分隔字段的文件。我想读取每一行,从行中提取字段,并将提取的 3 个值推送到一个对象中。最后,我应该得到一个对象数组。数组的长度应等于文件中的行数。文件中的所有信息都应该包含在对象中。
例如文件
abcd pqrs mnop
asdf asdf asdf
poiu poiu poiu
xcvx xcvb rtew
: : : :
: : : :
: : : :
这是我能想到的:
类定义
Class MyClass
{
string field1;
string field2;
string field3;
}
主要
String[] Content = File.ReadAllLines("file.txt");
var query = from line in Content
let Parts = line.Split(Separators,StringSplitOptions.RemoveEmptyEntries)
select new MyClass
{field1 = Parts[0],
field2 = Parts[1],
field3 = Parts[2],
};
如何从中获取对象的 List 或 IEnumerable?
【问题讨论】: