我正在将字符串从文本文件读取到string[]中。文本文件有顺序的数据,例如,第一个7元素被认为是一个记录,下一个7元素被认为是第二个记录,等等。
我需要将这些记录存储在对象中,所以最后我将有一个对象列表(List<Settlement>)。
我一直在寻找建议使用LINQ的解决方案,但我没有它的背景。
下面是一个示例文件
public static List<Settlement> ReadFromLogFile()
{
string filename = path + @"\BM_DB_MIGRATION.txt";
List<Settlement> settlements = new List<Settlement>();
if (File.Exists(filename))
{
string[] lines = File.ReadAllLines(filename);
// Reading 7 elements from lines into an object of Settelment at each iteration
// and store the object in a list of objects...
}
return settlements;
}发布于 2019-09-12 08:25:47
一个没有LINQ的例子:
public static List<Settlement> ReadFromLogFile()
{
string filename = path + @"\BM_DB_MIGRATION.txt";
List<Settlement> settlements = new List<Settlement>();
if (File.Exists(filename))
{
string[] lines = File.ReadAllLines(filename);
// Reading 7 elements from lines into an object of Settelment at each iteration
// and store the object in a list of objects...
var record = new List<string>(7);
foreach (var line in lines)
{
if (record.Count == 7)
{
// convert the lines to the Settlement data structure
var s = new Settlement(record);
settlements.Add(s);
record.Clear();
}
record.Add(line);
}
}
return settlements;
}https://stackoverflow.com/questions/57902447
复制相似问题