I am trying to convert a string to date format in .net Core but it gives me an error when converting it, I am implementing the Convert.ToDateTime(string) in both fields, one in my input variable and another in my ity attribute since it is also a string . What I want is to be able to convert it to a datetime so I can do a date filter from - to and be able to list the dates from that range
this is my code
public List<Stock_Cliente> visualizarMov(StockClienteFil stk)
{
DateTime fechaDesde = Convert.ToDateTime(stk.fec_mov_des);
DateTime fechaHasta = Convert.ToDateTime(stk.fec_mov_has);
var visualizarMov = _quimpacContex.Stock_Cliente.Where(x => x.stk_cli_des_alm.Contains(stk.des_alm) && x.stk_cli_des_mat.Contains(stk.des_mat)
&& x.stk_cli_num_lot.Contains(stk.num_lot) && x.stk_cli_ser_pre.Contains(stk.ser_pre) && x.stk_cli_buq.Contains(stk.cli_buq)
&& Convert.ToDateTime(x.stk_cli_fec_mov) >= fechaDesde && Convert.ToDateTime(x.stk_cli_fec_mov) >= fechaHasta).ToList();
return visualizarMov;
}
this is my Stock_Customer class
public class Stock_Cliente
{
[Key]
public int stk_cli_cod { get; set; }
public string stk_cli_cod_cen { get; set; }
public string stk_cli_des_cen { get; set; }
public string stk_cli_cod_cli { get; set; }
public string stk_cli_des_cli { get; set; }
public string stk_cli_cod_alm { get; set; }
public string stk_cli_des_alm { get; set; }
public string stk_cli_num_mat { get; set; }
public string stk_cli_des_mat { get; set; }
public string stk_cli_uni_med { get; set; }
public string stk_cli_num_lot { get; set; }
public string stk_cli_fec_mov { get; set; }
public string stk_cli_ser_pre { get; set; }
public decimal stk_cli_can { get; set; }
public string stk_cli_buq { get; set; }
public string stk_cli_fec_lle { get; set; }
public string stk_cli_hor_lle { get; set; }
public string stk_cli_fec_sal { get; set; }
public string stk_cli_hor_sal { get; set; }
public string stk_cli_est { get; set; }
public DateTime? stk_cli_fec_cre { get; set; }
public string? stk_cli_usu_cre_sap { get; set; }
public DateTime? stk_cli_fec_mod { get; set; }
public string? stk_cli_usu_mod_sap { get; set; }
}
this is the json i send you
{
"des_alm":"Tanque TKG13",
"des_mat":"ACEITE",
"num_lot":"CN-S",
"ser_pre":"Despacho",
"cli_buq":"PAB4168",
"fec_mov_des":"20/12/2019",
"fec_mov_has":"25/03/2019"
}
this is the error that generates me
System.InvalidOperationException: 'The LINQ expression 'DbSet<Stock_Cliente>()
.Where(s => s.stk_cli_des_alm.Contains(__stk_des_alm_0) &&
s.stk_cli_des_mat.Contains(__stk_des_mat_1) &&
s.stk_cli_num_lot.Contains(__stk_num_lot_2) &&
s.stk_cli_ser_pre.Contains(__stk_ser_pre_3) &&
s.stk_cli_buq.Contains(__stk_cli_buq_4) &&
Convert.ToDateTime(s.stk_cli_fec_mov) >= __fechaDesde_5 &&
Convert.ToDateTime(s.stk_cli_fec_mov) >= __fechaHasta_6)'
could not be translated. Additional information:
Translation of method 'System.Convert.ToDateTime' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.
Translation of method 'System.Convert.ToDateTime' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
this is my table in my bd
The method you are using cannot be translated by the EF (Convert.ToDateTime), so I recommend you do the following.
In this way, you would do the filter by date range in "memory", in the same way I think it is not necessary to do the Convert.ToDateTime, so you can do the query directly, without using the Convert.