jstuardo
jstuardo

Reputation: 4373

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"

Upvotes: 0

Views: 6072

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

You can easily Convert the value before calling LINQ query.

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

However, if you'd need to convert values in your query, you can use SqlFunctions and EntityFunctions classes. They contain a set of method that have corresponding SQL functions and can be used to do things like that.

Upvotes: 6

Daniel A. White
Daniel A. White

Reputation: 190905

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

Upvotes: 5

Related Questions