3
I have an application that generates the following results:
I would like to know how I can arrange the object in only 1 line, say have only one date, and within the date have the schedules, to have only 1 line for each date.
My object is created as follows:
namespace HBSIS.Models.WeatherViewModels
{
public class WeatherForecast
{
public ResponseList[] list { get; set; }
public ResponseCity city { get; set; }
}
public class ResponseCity
{
public string id { get; set; }
public string country { get; set; }
}
public class ResponseList
{
public DateTime dt_txt { get; set; }
public Main main { get; set; }
}
public class Main
{
[JsonProperty("temp")]
public string Temperature { get; set; }
}
}
With the following json:
{
"cod": "200",
"message": 0.0066,
"cnt": 40,
"list": [
{
"dt": 1560556800,
"main": {
"temp": 26.69,
"temp_min": 26.69,
"temp_max": 26.69,
"pressure": 1012.35,
"sea_level": 1012.35,
"grnd_level": 782.51,
"humidity": 19,
"temp_kf": 0
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "nuvens dispersas",
"icon": "03n"
}
],
"clouds": {
"all": 44
},
"wind": {
"speed": 0.55,
"deg": 307.139
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-06-15 00:00:00"
},
{
"dt": 1560567600,
"main": {
"temp": 16.48,
"temp_min": 16.48,
"temp_max": 16.48,
"pressure": 1016.15,
"sea_level": 1016.15,
"grnd_level": 785.76,
"humidity": 62,
"temp_kf": 0
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "nublado",
"icon": "04n"
}
],
"clouds": {
"all": 100
},
"wind": {
"speed": 4.65,
"deg": 297.502
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-06-15 03:00:00"
},
{
"dt": 1560578400,
"main": {
"temp": 17.43,
"temp_min": 17.43,
"temp_max": 17.43,
"pressure": 1015.73,
"sea_level": 1015.73,
"grnd_level": 784.88,
"humidity": 52,
"temp_kf": 0
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "nublado",
"icon": "04n"
}
],
"clouds": {
"all": 100
},
"wind": {
"speed": 0.84,
"deg": 229.68
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-06-15 06:00:00"
}
]
}
And here’s how I’m getting it today:
public async Task<WeatherForecast> GetCityWeatherAsync(string city)
{
string _apiKey = _configuration["API_Access_Weather:AccessKey"];
string _urlBase = _configuration["API_Access_Weather:UrlBase"];
try
{
using (var standings = new HttpClient())
{
var path = _urlBase + "q=" + city + "&units=metric&lang=pt&APPID=" + _apiKey;
standings.DefaultRequestHeaders.Accept.Clear();
standings.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage respToken = await standings.GetAsync(path);
WeatherForecast _weatherForecast = new WeatherForecast();
if (respToken.StatusCode == HttpStatusCode.OK)
{
string content = respToken.Content.ReadAsStringAsync().Result;
_weatherForecast = JsonConvert.DeserializeObject<WeatherForecast>(content);
}
else
Console.WriteLine(respToken.StatusCode.ToString() + " - " + respToken.ReasonPhrase);
return _weatherForecast;
}
}
catch (Exception ex)
{
throw new ApplicationException($"Algo deu errado ao tentar buscar o clima da cidade :'{city}' e a exceção foi lançada: '{ex}' ");
}
}
Through this object I would like to create a new +- in this way:
2019-06-15 {
00: 19
03: 18
06: 15
09: 22
....
}
I was told to do a DTO, but I don’t know how I can do it, I’ve even tried with LINQ
, but I don’t know how to group the values I receive from the array, described above.
What I’ve been able to do so far is group a part, but not exactly how I want it:
var retorno = _weatherForecast.list
.GroupBy(y => y.dt_txt.ToString("dd/mm/yyyy"))
.Select(x => new
{
date = x.FirstOrDefault().dt_txt.ToString("dd/mm/yyyy"),
hour = x.Select(y => y.dt_txt.Hour).ToList(),
temp = x.Select(y => y.main.Temperature).ToList()
});
show how you tried to generate the expected result
– Leandro Angelo
@Leandro Angelo, you’re talking about the table ? I managed it wrong ?
– Matheus
Ever tried to make a DTO for this endpoint?
– Washington A. Ramos
That’s what I’m trying to do, buddy
– Matheus