Dapper insert a list?

.net dapper

Question

I want to do a bulk insert. A is an ID and B is a list of IDs. My insert statement looks like this but it is wrong. How do I rewrite it to work? The only solution I can think of is using a foreach loop outside the statement

.Execute(@"insert into MyTable(a,b) select @a, @b", new {a, b})

Accepted Answer

Try this:

var abs = b.Select(id => new { a, b = id });
int numInserted = connection
    .Execute(@"insert into MyTable(a,b) VALUES(@a, @b)", abs);


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why