0

estoy tratando de convertir un string a formato fecha en .net Core pero me da error al convertirlo , estoy implementando el Convert.ToDateTime(string) en ambos campos , uno en mi variable de entrada y otro en mi atributo de tidad ya que también es un string . Lo que quiero es poder convertirlo en datetime para poder hacer un filtro de fecha desde - hasta y poder listar las fecha desde ese rango

este es mi código

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;
        }

este es mi clase Stock_Cliente

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; }
    }

este es el json que le envío

{
    "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"
}

este es el error que me genera

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.'

esta es mi tabla en mi bd

introducir la descripción de la imagen aquí

1
  • 1
    Por lo que veo a simple vista no es necesario que hagas el Convert.ToDateTime, de igual forma ese método no puede ser traducido por el EF, por lo que te recomiendo que hagas los filtros de fecha al final colocando un .ToList() el 25 mar. 2021 a las 16:02

1 respuesta 1

1

El método que estas utilizado no puede ser traducido por el EF (Convert.ToDateTime), por lo que te recomiendo que hagas lo siguiente.

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))
             .ToList()
             .Where(x=> Convert.ToDateTime(x.stk_cli_fec_mov) >= fechaDesde 
             && Convert.ToDateTime(x.stk_cli_fec_mov) >= fechaHasta).ToList();   
            return visualizarMov;

De esta manera el filtro por rango de fecha lo harías en "memoria", de igual forma creo que no es necesario hacer el Convert.ToDateTime, por lo que puedes hacer directamente la consulta, sin usar el Convert.

2
  • 1
    hola bro , pero no me retorna una lista y me esta marcando error en el código
    – C Aldair
    el 25 mar. 2021 a las 16:22
  • Si me falto el ToList() al final el 25 mar. 2021 a las 17:03

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.