Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse expression parameter if value has already been seen. #671

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PeterN
Copy link

@PeterN PeterN commented Oct 22, 2022

This is my solution for #670. This works in the same way for SqlExpression evaluation as SqlBuilder's ReuseParameters, except it is always on.

This is helpful for e.g. search parameters. Currently, query.Where(x => x.Field1.Contains(search) || x.Field2.Contains(search) || x.Field3.Contains(search)) will duplicate the search parameter each time, resulting in a WHERE clause looking like WHERE [Field1] LIKE '@0' OR [Field2] LIKE '@1' OR [Field3] LIKE '@2'.

With this change applied, this example would become WHERE [Field1] LIKE '@0' OR [Field2] LIKE '@0' OR [Field3] LIKE '@0'.

This works in the same way for SqlExpression evaluation as SqlBuilder's
ReuseParameters, except it is always on.
@schotime
Copy link
Owner

Its a tiny bit more complex than that, because if the column type you are querying against is different but the value is the same, then you will get errors, as the parameter can't be reused against multiple types.

This is why it was turned off, as it can't be controlled, however we could be smarter in the linq provider as you know the type you are querying against and could reuse ones of the same type. At least you might have a better chance of getting it right anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants