none
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' RRS feed

  • Question

  • Hello,

    I want to convert string to Int32 in LINQ Entity but its not working .

    This is my code.

    TariffChild _AdditionalQuantity = _Context.TariffChilds.Where(x => (Convert.ToInt32(x.FromBracket) <= Convert.ToInt32(Quantity)) && (Convert.ToInt32(x.ToBracket) >= Convert.ToInt32(Quantity))).FirstOrDefault();

    Monday, May 16, 2016 3:44 PM

Answers

  • Hi M.Uzair,

    >>I want to convert string to Int32 in LINQ Entity but its not working .

    we could not use method 'Convert.ToInt32' on LINQ to Entities, because EF does not know how to convert the method Convert.ToInt32 to SQL, please convert the variable before you use EF LINQ to entities. like this:

    int iQuantity = Convert.ToInt32(Quantity);
    
    TariffChild _AdditionalQuantity = _Context.TariffChilds.Where(x => (x.FromBracket <= iQuantity ) && (x.ToBracket >= iQuantity )).FirstOrDefault();

    In addition, you could convert LINQ to entities to memory, then you could use the method in memory. like this:

    var result = db.TariffChilds.ToList().Where(x => (Convert.ToInt32(x.FromBracket) <= Convert.ToInt32(Quantity)) && (Convert.ToInt32(x.ToBracket) >= Convert.ToInt32(Quantity))).FirstOrDefault();

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, May 17, 2016 1:22 AM
    Moderator