Using tuple to return single row with 2 values from NPoco returns error

364 views Asked by At

I use NPoco and I would like to have a query return two results - not a single object with two properties, but just two sums of some table. I was hoping to retrieve this as a tuple, but NPoco fails on that.

I tried this:

Tuple<decimal, int> res = db.Single<Tuple<decimal, int>>(
                                  "select sum(alpha), sum(beta) from gamma");

but this gave me this error:

Exception: Cannot create POCO 'System.Tuple'2[[decimal...][int...]]  It may have no parameterless constructor or be an interface or abstract class without a Mapper factory.

Is there a way to retrieve more than one value without needing to create a class for the return object?

1

There are 1 answers

0
Hashka On

Ok, same problem here, and I countered it. Npoco has problems with Tuples.

Turn that :

Tuple<decimal, int> res = db.Single<Tuple<decimal, int>>("select sum(alpha), sum(beta) from gamma");

Into that :

List<(decimal, int)> res = db.Query<dynamic>("select sum(alpha) as col1, sum(beta) as col2 from gamma").Select(x => ((decimal)x.col1, (int)x.col2)).ToList();