Given a line: тест|5 проверка|2 проверка (there are many similar lines)

How to display the entire line corresponding to the LINQ operation: a digit from the second segment >= 5

Example:

 тест|1 проверка тест|5 проверка тест|4 проверка тест|8 проверка 

Result:

 тест|5 проверка тест|8 проверка 

Need an option using LINQ operations.

    1 answer 1

    Well, like this:

     var s = "тест|5 проверка|2 проверка"; Console.WriteLine( string.Concat( s.SkipWhile(c => c != '|') // пропустить первый сегмент .Skip(1) // попустить разделитель .TakeWhile(c => c != '|') // взять только второй сегмент .Where(char.IsDigit) // отфильтровать цифры .Select(c => int.Parse(c.ToString())) // перевести в числа .Where(n => n >= 5))); // отфильтровать те, которые не меньше пяти 

    For the case of returning a whole string, it's also simple:

     var lines = new[] { "тест|5 проверка|2 проверка", "тест|1 проверка", "тест|5 проверка", "тест|4 проверка", "тест|8 проверка" }; Console.WriteLine(string.Join("\n", lines.Where( line => line.SkipWhile(c => c != '|') .Skip(1) .TakeWhile(c => c != '|') .Where(char.IsDigit) .Select(c => int.Parse(c.ToString())) .Any(n => n >= 5)))); 
    • Thanks for the answer, but because I solved this problem literally a minute after the creation of the topic, then I changed my question in the subject. Thanks for the option, I'll throw it out, your more correct one. - Max
    • @Maxim: So you now have a rowset at the entrance? - VladD
    • Why now? There were many of them in the previous question (in the first post it was written). Just now we need to output the entire line, if the result matches >= 5 - Max
    • Thank you very much. - Maxim
    • @ Maxim: Please! Hope that helped. - VladD