DynamicParameters works with DbString but not with IEnumerable<DbString>
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/.
I'm looking forward to see how and when it will be solved. In my case I have big amount of data and implicit conversion in the sql results in a performance degragation to the state that query instead of 10 seconds takes 50.. I used workaround from #789 that was made in 2017 and still not solved (closed state rather than solving it).