jstuardo jstuardo - 1 year ago 55
C# Question

Conversion methods do not work in LINQ?

According to this page http://msdn.microsoft.com/en-us/library/bb399342.aspx, conversion methods should work, however, they are not working.

I have this code:

var funcionario = (from f in _db.Funcionario
where f.FunId == Convert.ToDecimal(funId)
select f).FirstOrDefault();

but a runtime error occurs concerning ToDecimal conversion.

"LINQ to Entities no reconoce el método 'System.Decimal
ToDecimal(System.String)' del método, y este método no se puede
traducir en una expresión de almacén."

Any help will be appretiated. I am in .NET 4.5.

EDIT: For those who need translation of the error message:
"LINQ to Entities does not recognize method 'System.Decimal ToDecimal(System.String)' of the method, and this method cannot be translated into a warehouse expression"

Answer Source

Convert funId before hand. The linq expressions in there are converted to SQL equvilants by EF at runtime. Not all functions are supported.