C#:返回通用列表的问题

时间:2018-10-29 12:32:17

标签: c# asp.net

我有此代码:

型号:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyAplication.Models
{
  public class Result
  {
    public string MarketName { get; set; }
    public double High { get; set; }
    public double Low { get; set; }
    public double Volume { get; set; }
    public double Last { get; set; }
    public double BaseVolume { get; set; }
    public DateTime TimeStamp { get; set; }
    public double Bid { get; set; }
    public double Ask { get; set; }
    public int OpenBuyOrders { get; set; }
    public int OpenSellOrders { get; set; }
    public double PrevDay { get; set; }
    public DateTime Created { get; set; }
  }

  public class RootInfoCoins
  {
      public bool success { get; set; }
      public string message { get; set; }        
      public List<Result> result { get; set; }        
  }
}

这堂课

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using PeekAndGoApp.Models;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using Newtonsoft.Json;
    using System.Collections;

    namespace MyAplication.Controllers
    {
        [Produces("application/json")]
        [Route("api/[controller]")]
        [ApiController]
        public class CoinsController : ControllerBase
        {
            [HttpGet]
            public async Task<IEnumerable<RootInfoCoins>> Get()
            {
                string Baseurl = "https://bittrex.com";
                string Parameters = "api/v1.1/public/getmarketsummaries";
                RootInfoCoins CoinsInfo = new RootInfoCoins();                  

                using (var client = new HttpClient())
                {
                    //Passing service base url  
                    client.BaseAddress = new Uri(Baseurl);

                    client.DefaultRequestHeaders.Clear();
                    //Define request data format  
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //Sending request to find web api REST service resource GetAllCoins using HttpClient  
                    HttpResponseMessage Res = await client.GetAsync(Parameters);

                    //Checking the response is successful or not which is sent using HttpClient  
                    if (Res.IsSuccessStatusCode)
                    {
                        //Storing the response details recieved from web api   
                        string CoinResponse = Res.Content.ReadAsStringAsync().Result;                                               
                        CoinsInfo = JsonConvert.DeserializeObject<RootInfoCoins>(CoinResponse);
                    }
                    return CoinsInfo.result;                        
                }
            }
        }
    }

我的问题是我试图返回我的数据并获得此信息:

  

“无法将类型'System.Collections.Generic.List'隐式转换为   'System.Collections.Generic.IEnumerable'。   存在显式转换(您是否缺少演员表?)”

我是C#的新手(正在学习)。我已经做了很多研究,但无法解决我的问题。也许我不了解这种语言的概念。拜托,有人可以帮我吗?

PS:我已经附上了一张图片,以便您可以看到我接收到的数据的“格式”。 Debugging

非常感谢。

俊。

2 个答案:

答案 0 :(得分:0)

Get()期望返回IEnumerable<RootInfoCoins>,但是您正在尝试返回List<Result>。您可以通过将签名和RootInfoCoins语句更改为

来返回从JsonConvert创建的return
public async Task<RootInfoCoins> Get()
{
    //...
    return coinsInfo;
}

或通过将签名更改为{p>返回List<Result>

public async Task<IEnumerable<Result>> Get() { }

答案 1 :(得分:0)

    [HttpGet]
    public async Task<IEnumerable<Result>> Get()
    {
        string Baseurl = "https://bittrex.com";
        string Parameters = "api/v1.1/public/getmarketsummaries";
        RootInfoCoins CoinsInfo = new RootInfoCoins();

        using (var client = new HttpClient())
        {
            //Passing service base url  
            client.BaseAddress = new Uri("");

            client.DefaultRequestHeaders.Clear();
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Sending request to find web api REST service resource GetAllCoins using HttpClient  
            HttpResponseMessage Res = await client.GetAsync(Parameters);

            //Checking the response is successful or not which is sent using HttpClient  
            if (Res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api   
                string CoinResponse = Res.Content.ReadAsStringAsync().Result;
                CoinsInfo = JsonConvert.DeserializeObject<RootInfoCoins>(CoinResponse);
            }

            return CoinsInfo.result;

        }

更改此内容,因为CoinsInfo.result是List类型,因此,您更改返回类型

  public async Task<IEnumerable<Result>> Get()