Possuo uma aplicação que gera os seguintes resultados:
Gostaria de saber como posso organizar o objeto em apenas 1 linha, digamos ter apenas uma data, e dentro da data ter os horários, para ter apenas 1 linha para cada data.
Meu objeto é criado da seguinte forma:
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; }
}
}
Com o seguinte 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"
}
]
}
E aqui está como estou recebendo ele hoje:
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}' ");
}
}
Através desse objeto gostaria de criar um novo +- dessa forma:
2019-06-15 {
00: 19
03: 18
06: 15
09: 22
....
}
Me indicaram fazer um DTO, mas não sei como posso fazer isso, já tentei até com LINQ
, mas nao sei como agrupar os valores que recebo do array, descrito acima.
Oque consegui fazer até agora é agrupar uma parte, mas não exatamente como quero:
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()
});