3

quiero hacer un select con where or where y estoy tratando de hacer de la siguiente manera, pero ya cuando ejecuto este método

  public JsonResult JSONInfoFinTriMonths3()
    {
        using (DBEnt db = new DBEnt())
        {
            //Select(m => m.Name).Distinct();
            var categorias = db.HomeInfoFinanciera.Where(s => s.month == 7 && s.month == 8 && s.month == 9);



            return Json(categorias, JsonRequestBehavior.AllowGet);
        }
    }

Aquí tengo el query que me genera

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[created_at] AS [created_at], 
[Extent1].[lastupdate] AS [lastupdate], 
[Extent1].[id_userCreated] AS [id_userCreated], 
[Extent1].[tipo] AS [tipo], 
[Extent1].[path] AS [path], 
[Extent1].[year] AS [year], 
[Extent1].[month] AS [month], 
[Extent1].[day] AS [day]
FROM [dbo].[HomeInfoFinanciera] AS [Extent1]
WHERE [Extent1].[month] IN (7,8,9)

que justo me devuelve todos los datos que deseo.

Aquí estoy llamando al método

  $.ajax({
    url: "/Home/JSONInfoFinTriMonths3",
    type: "GET",
    dataType: "json",
    //contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log(response)
        //alert(response);

        var resplength = response.length;
        for (let i = 0; i <= resplength; i++) {

            document.getElementById('months1div')
                .innerHTML += `
                    <li data-filter=".filter-${response[i].year}">
                    ${response[i].year}</li>
                `;
        }
    },

    error: function (jqXHR, textStatus, err) {
        //$('#myModalLabel').append(`error: - ${err}`);
        console.log(jqXHR);
        console.log(textStatus);
        console.log(err);

        alert(jqXHR.responseJSON + "JSONInfoFinYears");

    }

});

Pero obtengo el error

alert(jqXHR.responseJSON + "JSONInfoFinYears");

5
  • 1
    Por ningún lado veo el "OR". ¿Has probado la herramienta Postman?
    – fredyfx
    el 24 sep. 2019 a las 19:07
  • Estás seguro de que Where(s => s.month == 7 && s.month == 8 && s.month == 9) se traduce en WHERE [Extent1].[month] IN (7,8,9)?. Me lo podrías confirmar por favor. el 24 sep. 2019 a las 19:19
  • 1
    @RafaelAcosta pues entiendo que && es and y que || es or, pero con ambos me tira error el 24 sep. 2019 a las 19:31
  • Has probado entonces: Where(s => s.month == 7 || s.month == 8 || s.month == 9) ? el 24 sep. 2019 a las 19:33
  • si si, pero nada el 24 sep. 2019 a las 19:48

1 respuesta 1

2

Si es de interés para alguien aprendiendo, teminé haciendo esto con Native SQL https://www.entityframeworktutorial.net/Querying-with-EDM.aspx

   public JsonResult JSONInfoFinTriMonths3()
    {
        using (DBEnt db = new DBEnt())
        {
            //Select(m => m.Name).Distinct();
            //var categorias = db.HomeInfoFinanciera.Where(s => s.month == 7 || s.month == 8 || s.month == 9);
            var categorias = db.HomeInfoFinanciera.SqlQuery
                ("Select * from HomeInfoFinanciera where month = 7 or month = 8 or month = 9")
                .ToList<HomeInfoFinanciera>();
            return Json(categorias, JsonRequestBehavior.AllowGet);
        }
    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

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