[SalesForce] how to escape wild character _ underscore in LIKE SOQL Clause

I have this query
Select Id, Email, ContactId, Username from User where Username Like '\_%'
works perfectly with Devloper console and give me users whose username starts with _

I am using this code in apex, but there its not giving the desired result.

From salesforce docs

The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
Don’t use the backslash character in a search except to escape a special character.

PS : I have tried escaping double backslash \\_ , four backslash \\\\_, [_] . None worked. and not escaping also.

Best Answer

In my tests, a double backslash escape has provided the desired results:

List<User> users = (List<User>)Database.query('Select Id, Email, ContactId, Username  from User where Username Like \'\\_%\'');