NPoco icon indicating copy to clipboard operation
NPoco copied to clipboard

WhereSql does not put parentheses around statement leading to unexpected results

Open ueler opened this issue 1 year ago • 0 comments

Hi,

we've been using WhereSql in our project and we noticed it does not put parentheses around the statement. This can lead to unexpected behaviour.

Example

IQueryProvider<User> query = DbAccess.DB.Query<User>();
query = query.Where(u => u.Email.Contains("o"));
query = query.WhereSql("firstname LIKE '%o%' OR lastname LIKE '%o%'");

returns all users where lastname contains 'o', even though it was constrained to email containing 'o'. The rendered query does not have a parentheses around the statement "firstname LIKE '%o%' OR lastname LIKE '%o%'", rendering the email condition useless.

This is in contrast to the following example using pure Where statements

 IQueryProvider<User> query = DbAccess.DB.Query<User>();
query = query.Where(u => u.Email.Contains("o"));
query = query.Where(u => u.FirstName.Contains("o") || u.LastName.Contains("o"));

Here the rendered query does have parentheses around the rendered second condition, not rendering the email condition useless.

This leads to different results, although the intention of both statements was the same.

ueler avatar Aug 28 '24 15:08 ueler