c# - Can Dapper's support for IEnumerables simplify an INSERT INTO SELECT? -
i'm trying use dapper's support ienumerable parameter objects perform insert into, running trouble.
my insert statement based on select:
insert [tbl] ([col1], [col2], [col3]) output inserted.[id] select [col1] - 1, [col2] + 1, @newval [tbl] [id] in @id , [col1] > 1
i'm trying use query<t>()
follows:
int[] ids = ...; var sql = @"insert ..."; // above var parameters = ids.select(id => new { id = id, newval = generatenewvalueforeachrow() }).toarray(); ienumerable<int> newids = dbconnection.query<int>(sql, parameters);
but i'm getting invalidoperationexception: "an enumerable sequence of parameters (arrays, lists, etc) not allowed in context"
i have use query<t>()
instead of execute<t>()
(or e.g. executescalar<t>()
) because want ienumerable
of newids. (at least, think have to... right?)
further, want use different @newval
each row inserted, otherwise use
var parameters = new { ids = ids, newval = generatenewrepeatedvalue() };
the obvious answer foreach
on ids
:
// transactions , async omitted foreach (var id in ids) { var newid = dbconnection.executescalar<int>( <slightly modified sql>, new { id = id, newval = generatenewvalueforeachrow() } ); }
but i'm hoping dapper's built-in functionality can me. can it? thanks!
Comments
Post a Comment