简体   繁体   English

C#Linq。包含字符串列表

[英]C# Linq .Contains list of string

How check in where line.Contains("One") more than one string? 如何检查在哪里line.Contains(“ One”)多于一个字符串? For example how select file contains line with text element of "names" List. 例如,选择文件如何包含带有“名称”列表的文本元素的行。

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
            };

You can probably try to use something like this: 您可能可以尝试使用以下方法:

line.Intersect(names).Any()

Although, I'm not sure whether it works inside a Linq expression? 虽然,我不确定它是否可以在Linq表达式中使用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM