3
Hello, I have a project that calls a WebApi
which brings the data from the database, but one of the fields in the database is of the type DateTime
and in my project, I declared as String (I can change, no problem), what I want is the following: I wanted that when it brings this value, it takes the part of horas
and just let the data
The field is: Cba_dt_abertura
How can I do a conversion, or maybe take that part, follows images:
How are you:
As it should be:
My project:
public async Task<IActionResult> Index()
{
Uri BaseAdress = Services.Token.BaseAdress;
string strToken = Services.Token.strToken;
List<ContaBancariaModel> ListaContaBancaria;
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = BaseAdress;
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", strToken);
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("Application/Json"));
using (HttpResponseMessage response = await httpClient.GetAsync("/api/ContaBancaria/findAll"))
{
response.EnsureSuccessStatusCode();
string resul = await response.Content.ReadAsStringAsync();
ListaContaBancaria = JsonConvert.DeserializeObject<List<ContaBancariaModel>>(resul);
}
}
return View(ListaContaBancaria);
}
Method in the Webapi:
[HttpGet]
[Route("findAll")]
public HttpResponseMessage findAll()
{
try
{
var result = new HttpResponseMessage(HttpStatusCode.OK);
var bancos = bdprincipalEntities.Contas_Bancarias.Select(
x => new {
Cba_codigo = x.Cba_codigo,
Cba_numero = x.Cba_numero,
Cba_tipo_conta = x.Cba_tipo_conta,
Cba_dt_abertura = x.Cba_dt_abertura,
Cba_gerente = x.Cba_gerente,
Emp_codigo = x.Emp_codigo,
Cba_situacao = x.Cba_situacao.Equals("A") ? "ATIVO" : "DESATIVADO"
}).ToList();
result.Content = new StringContent(JsonConvert.SerializeObject(bancos));
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
return result;
}
catch (Exception)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
}
I don’t know if you tested it, but the visual studio says that Datetime does not contain a definition for 'Toshortdatestring'
– Jeff Henrique
When I add the stitch I can only use the following methods: "Equals, Gethashcode, Gettype, Getvalueordefault and Tostring".
– Jeff Henrique
Method documentation: https://docs.microsoft.com/pt-br/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.7.2
– Barbetta
John’s answer is correct, but it is important to note that the Toshortdatestring() method is applied to properties of the Datetime type. Jeff could not find the method probably because his Cba_dt_aperture property is of the string type. Jeff, as you reported, you can change the type of your property. Modify().
– Sérgio Lopes
As @Sérgiolopes said, the method is only available for the Datetime type
– João Paulo Amorim
Oh yes, I did not know that, thank you for your reply
– Jeff Henrique
{"LINQ to Entities does not recognize the method 'System.String Toshortdatestring()' method, and this method cannot be Translated into a store Expression." } I get this error when using this method
– Jeff Henrique
You switched the attribute x.Cba_dt_aperture to Datetime in your Entity? because the error says that it is trying to use the method in a string.
– João Paulo Amorim
The attribute Cba_dt_entity opening needs to be Datetime, since the Cba_dt_model opening has to be string.
– João Paulo Amorim
I made the changes you said, but gave me error again, but I found a solution, your method is right, must be some error here, thanks for the help, I will post the solution I found
– Jeff Henrique