包含字符串列表

本文关键字:列表 字符串 包含 | 更新日期: 2023-09-27 18:05:04

如何检查line.Contains("One")多于一个字符串?例如,如何选择包含"names"列表的文本元素的文件。

private void button_Click(object sender, RoutedEventArgs e)
{
  List<string> names = new List<string>() { "One", "Two", "Three" };
  try
  {
    var files = from file in Directory.EnumerateFiles(@"D:'Logs'", "*.log", SearchOption.AllDirectories)
                from line in File.ReadLines(file)
                where line.Contains("One")
                select new
                {
                  File = file,
                  Line = line
                };
    foreach (var f in files)
    {
      Debug.WriteLine("{0}'t{1}", f.File, f.Line);
    }
    //MessageBox.Show(files.Count().ToString() + " record found.");
    }
  catch (UnauthorizedAccessException UAEx)
  {
    Console.WriteLine(UAEx.Message);
  }
  catch (PathTooLongException PathEx)
  {
    Console.WriteLine(PathEx.Message);
  }
}

包含字符串列表

var files = from file in Directory.EnumerateFiles(@"D:'Stary komp'Logi'Logs2'", "*.log", SearchOption.AllDirectories)
            from line in File.ReadLines(file)
            where names.Any(name => line.Contains(name))
            select new
            {
              File = file,
              Line = line
            };

你可以试着这样写:

line.Intersect(names).Any()

虽然,我不确定它是否在Linq表达式内工作?