NPoco icon indicating copy to clipboard operation
NPoco copied to clipboard

Reuse expression parameter if value has already been seen.

Open PeterN opened this issue 2 years ago • 1 comments

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'.

PeterN avatar Oct 22 '22 00:10 PeterN

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.

schotime avatar Oct 22 '22 01:10 schotime