string query = "A|B";
The output of the next 2 lines are equal to "%A%|%B%"
whereas "A%B" is expected!
query = Regex.Replace(query, "|", "%");
query = Regex.Replace(query, @"|", "%");
Why?
The |
is a special character of regexes, it means "or". You have to escape it.
query = Regex.Replace(query, @"\|", "%");