Filtering a line out of a string c#
c#
Solution
You can check all lines at once using `File.ReadLines()` method and LINQ:
var firstAnderlecht = File.ReadLines(textBox1.Text).FirstOrDefault(s => s.Contains("Anderlecht"));
if (firstAnderlecht != null) {
label5.Text = firstAnderlecht;
}
The `ReadLines` produces an enumerable of lines from the file; the `FirstOrDefault` method lets you apply a condition to all lines of the file without a loop, and pick the first line where the condition applies.
Problem
I want to read a .txt file in c# and filter a line out of the string and only show that line. If the match is on the first line, i get a good output using streamreader.ReadLine. But if it's on the second line, i need to get it filtered. (i tought by creating a ReadLine loop?) Thanks in advance ``` private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { StreamReader sr = new StreamReader(textBox1.Text); string BoxLM1 = sr.ReadLine(); if (comboBox3.Text == "Anderlecht") { if (BoxLM1.Contains("Anderlecht")) { label5.Text = BoxLM1; } else { string BoxLM2 = sr.ReadToEnd(); MessageBox.Show(BoxLM2); } ```