【问题标题】:Using tuple to return single row with 2 values from NPoco returns error使用元组从 NPoco 返回具有 2 个值的单行返回错误
【发布时间】:2019-09-19 16:36:33
【问题描述】:

我使用 NPoco,我希望查询返回两个结果 - 不是具有两个属性的单个对象,而是某个表的两个总和。我希望将其作为元组检索,但 NPoco 失败了。

我试过了:

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

但这给了我这个错误:

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.

有没有一种方法可以在不需要为返回对象创建类的情况下检索多个值?

【问题讨论】:

    标签: npoco


    【解决方案1】:

    好的,同样的问题,我反驳了。 Npoco 在元组方面存在问题。

    转动那个:

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

    进入那个:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 2014-04-27
      • 1970-01-01
      • 2015-06-19
      • 2016-09-03
      相关资源
      最近更新 更多