DynamicParameters works with DbString but not with IEnumerable<DbString>
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
If the value of a DynamicParameters is a ICustomQueryParameter that implementation's AddParameter(IDbCommand, string) implementation will be used. This means that DynamicParameters transparently wraps DbString; this works:
var id = new DbString() { IsAnsi = true, Value = "foo" };
var p1 = new DynamicParameters();
pn.Add("@id", id, DbType.AnsiString);
conn.QueryAsync<int>("select 1 from tbl where id = @id", p1).
This does not scale to values of type IEnumerable<ICustomQueryParameter>; this does not work:
var ids = new DbString[] { id };
var pn = new DynamicParameters();
pn.Add("@ids", ids, DbType.AnsiString);
conn.QueryAsync<int>("select 1 from tbl where id in @ids", pn).
This relates to
- the documented
inoperator support, whose combination with string types is non-obvious, difficult to troubleshoot, and dangerous - #789, which asserts that the way to resolve the above is with an anonymous type whose value is some
IEnumerable<DbString> - #1672, which generalizes the built-in support for
IEnumerable<DbString>toIEnumerable<ICustomQueryParameter>.
As a consequence, I can find no way to combine the in operator with either DynamicParameters or, in some other way, DbType. I would like to be able to do so because DbString is much more cumbersome to work with than the core DbType (or SqlDbType) and does not generalize to other types, and because it is far too easy to write perfectly sensible, non-functional code as is.
Up until such a time this gap is closed, the documentation should feature much more prominent examples of how to use the in operator with varchar (and char) columns and still avoid https://www.brentozar.com/blitz/implicit-conversion/.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two DynamicParameters examples, then trace the IEnumerable and DbString handling described in the issue. Read the README sections on list support and ANSI strings/varchar, along with issues #789 and #1672; done should cover the requested list-parameter behavior or clearly document the varchar/char workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100