web api不想建实体,用dynamic类型返回数据

如果一个项目中实体太多也挺麻烦, 虽然说面向对象编程,但是如果只返回两个字段也需要建一个实体那多麻烦, 基于这个问题dynamic就很好的解决这些事

 

 public static List<dynamic> BugSellTop5Desc(int coinId, int currencyId,int  type)
        {
            var sql = string.Empty; 
            var list = SqlDapperHelper.ExecuteReaderReturnList<dynamic>(sql, new
            {
                CurrencyID = currencyId,
                CoinID = coinId
            });
            return list;
        }

调用

 

 var list = SellOutService.BugSellTop5Desc(coinId, currencyId, type);
                resultMsg.statue = Convert.ToInt32(StatueEnum.Success);
                resultMsg.data = list.Select(item=>new
                {
                    Price=item.Price.ToString(),
                    TotalNum= item.TotalNum.ToString(),
                });    //或者不需要select一样可以,  这里只是前端要求用string类型故处理下

 

当然话会所话来,  dynamic一样可以用object代替, 

 public static List<object> BugSellTop5Desc(int coinId, int currencyId,int  type)
        {
            var sql = string.Empty; 
            var list = SqlDapperHelper.ExecuteReaderReturnList<object>(sql, new
            {
                CurrencyID = currencyId,
                CoinID = coinId
            });
            return list;
        }

 

效果一样........时间很紧,干活去了

 

转载于:https://my.oschina.net/objectboy/blog/1516624

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值