在下面的代码中,如何将 myLine 声明为公共(全局)变量?问题是我不能使用关键字“var”。
public static IEnumerable<string> ReadLines(StreamReader reader)
{
while (!reader.EndOfStream)
{
yield return reader.ReadLine();
}
}
private void Filter1(string filename)
{
using(var writer = File.CreateText(Application.StartupPath + "\\temp\\test.txt"))
{
using (var reader = File.OpenText(filename))
{
int[] Ids = { 14652, 14653, 14654, 14655, 14656, 14657, 14658, 14659, 14660 };
var myLine = from line in ReadLines(reader)
where line.Length > 1
let id = int.Parse(line.Split('\t')[1])
where Ids.Contains(id)
let m = Regex.Match(line, @"^\d+\t(\d+)\t.+?\t(item\\[^\t]+\.ddj)")
where m.Success == true
select new { Text = line, ItemId = id, Path = m.Groups[2].Value };
foreach (var id in myLine)
{
writer.WriteLine("Item Id = " + id.ItemId);
writer.WriteLine("Path = " + id.Path);
writer.WriteLine("\n");
}
}
}
}
我想这样做,因为我必须找到一种方法来访问该 ienumerable 以供以后使用。