我正在尝试迭代sting []并且只获取尊重条件“Contains”的记录。
我一直在尝试这个:
var lines = File.ReadAllLines("C:\\temp\\" + ReportPushFile("fileToSend_1.txt")).Where(i => i.Contains("BILL"));
但这会在没有过滤的情况下再次返回所有记录。怎么了?
class Program : Logger
{
public static FTPclient Ftp = new FTPclient("ftp://smsftp.mobyt.it/", "C17053_004", "i7z0dx5b");
static void Main(string[] args)
{
List<Sms> sms = new List<Sms>();
sms.Add(new Sms() { recipient = "+393664528452" });
var lines = File.ReadAllLines("C:\\temp\\" + ReportPushFile("fileToSend_1.txt")).Where(i => i.StartsWith("BILL"));
// ...
}
public static string ReportPushFile(string NomeFile)
{
try
{
String LocalFileName = Ftp.ListDirectory("/reports/").Where(f => f.Contains(NomeFile)).OrderBy(x => new FileInfo(x).CreationTime).Max();
Ftp.Download("/reports/" + LocalFileName, "c:\\temp\\" + LocalFileName, true);
return LocalFileName;
}
catch(Exception ex)
{
return string.Empty;
}
}
}
答案 0 :(得分:1)
您的代码应该有效。让我们将其拆分并使类型明确:
string path = "C:\\temp\\" + ReportPushFile("fileToSend_1.txt");
string[] readLines = File.ReadAllLines(path);
IEnumerable<string> lines = readLines.Where(i => i.Contains("BILL"));
您现在可以设置一些调试点并验证:
readLines
包含一系列行。lines
仅包含其中包含BILL
的行。