[英]C#: Issues to return a generic list
I have this code: 我有以下代码:
Model: 模型:
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; }
}
}
And this class: 和这个类:
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;
}
}
}
}
My problem is that i try to return my data and get this: 我的问题是我尝试返回我的数据并获取此信息:
"Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)" “无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(是否缺少转换?)”
I'm new in C# (i'm learning). 我是C#的新手(正在学习)。 I've done a lot of research and i can't resolve my problem. 我已经做了很多研究,但无法解决我的问题。 Maybe I'm not understanding the concepts of this language. 也许我不了解这种语言的概念。 Please, can someone help me? 拜托,有人可以帮我吗?
PS: I have attached an image so that you can see "the format" of the data I am receiving. PS:我已附上一张图片,以便您可以看到我接收到的数据的“格式”。
Thanks a lot. 非常感谢。
Jhon. 俊
Get()
expects to return IEnumerable<RootInfoCoins>
, but you are trying to return List<Result>
. Get()
期望返回IEnumerable<RootInfoCoins>
,但是您正在尝试返回List<Result>
。 You can either return the RootInfoCoins
created from JsonConvert
by changing the signature and return
statement to 您可以通过将签名和return
语句更改为从RootInfoCoins
创建的JsonConvert
来return
public async Task<RootInfoCoins> Get()
{
//...
return coinsInfo;
}
Or return List<Result>
by changing the signature to 或通过将签名更改为List<Result>
来返回
public async Task<IEnumerable<Result>> Get() { }
[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;
}
change this because CoinsInfo.result is a List type so, you change the return type 更改此内容,因为CoinsInfo.result是List类型,因此,您更改返回类型
public async Task<IEnumerable<Result>> Get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.