0

How check in where line.Contains("One") more than one string? 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);
  }
}
3
  • 1
    Do you mean line.Contains("One") || line.Contains("names")? Commented Sep 1, 2016 at 12:22
  • 1
    Sorry, can you please clarify your question a little bit. The English is a little unclear. Commented Sep 1, 2016 at 12:23
  • You need to use any instead of Contains Commented Sep 1, 2016 at 12:23

2 Answers 2

4
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
            };
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Works fine.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.