简体   繁体   中英

How can I do a batch select in Dapper?

I have a table with a composite primary key of two columns. In Dapper .NET I am using .Query by passing in my select statement, along with an array of the parameters. I found out that dapper only expects a single object for parameters in SELECT statements, unlike Execute for INSERT and UPDATE, where I could do this:

var batchParams = new List<object>();
batchParams.add(new 
    {
    ID = 50,
    Lang = 40
    });
batchParams.add(new 
    {
    ID = 20,
    Lang = 31
    });

And then I could just pass in this array to my execute call. Essentially, I want to run multiple select statements (each will retrieve one row), and then get the result back as an array of results. Is this possible in dapper?

It isn't built in, but you could probably add an extension method that does something like:

foreach(var val in input)
    foreach(var row in conn.Query<T>(sql, val))
        yield return row;

This could also probably be achieved via SelectMany in LINQ-to-Objects:

var  combined = batch.SeletMany(x => conn.Query<T>(sql, x));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM