1

I'd migrated my entity 6.0 project from SQL Server to PostGreSQL. With SQL Server, these kind of convertion on my queries used to work properlly

Module.cs

return (
    from m in objDB.Modules
    orderby m.ID
    select new
    {
        ID = m.ID,
        Name = m.Name,
        Status = DB_Function.Convert.ToInt32( m.Status )
    }
);

PS: Status is a boolean type

DB_Function.cs

[System.Data.Entity.DbFunctionAttribute( "Business.Database", "ToInt32" )]
public static Int32 ToInt32( Boolean Val )
{
    return System.Convert.ToInt32( Val );
}

However, when I migrated to PostgreSQL (and therefore changed my EDMX), these kind of conversion don't execute anymore:

The specified method 'Int32 ToInt32(Boolean)' on the type 'DB_Function+Convert' cannot be translated into a LINQ to Entities store expression.

This error is related with PostGre (like int4 and not int32) or I'm missing something?

Thanks in advance.

DavidG
  • 113,891
  • 12
  • 217
  • 223
BernardoMorais
  • 571
  • 2
  • 6
  • 14
  • Not an answer to your question, but why are you converting to `Int32` if `Status` is a boolean? – DavidG Jun 11 '15 at 14:03
  • Because I need it with 0 and 1. In SQL Server, there is no type boolean, just bit, so this conversion was just for the bit value became int32. Although, in PostGre there is a type boolean, but all the code was programmed to work with 0 or 1, so I'm trying not to modify it drastically. – BernardoMorais Jun 11 '15 at 14:09
  • Could you change the method to be `return Val ? 1 : 0;` instead? – DavidG Jun 11 '15 at 14:11
  • I try with this but still returning the same error message. Now I'm with more doubts, i thought that your solution would fit like a glove. Maybe the problem is with the calling of the method. Thanks anyway for that. Edit: I have modified the linq itself and works. Thanks a lot – BernardoMorais Jun 11 '15 at 14:17
  • Not to change the subject, but if you used a bit field in Sql Server, it would have by mapped it to a boolean for you in Entity Framework. And the function you have should work (I think, never used postgres with EF) *IF* postgres has a function called "ToInt32". Did you not create that function in the database? – Robert McKee Jun 11 '15 at 15:45
  • I guess that I didn't undestand your comment but yes, that method that I shown was created by me. There is no native "ToInt32" even with SQL Server. – BernardoMorais Jun 11 '15 at 16:01

2 Answers2

0

For understanding what type of functions we can use inside LINQ to Enitities : go through below linK : linq to entities doesn't recognize a method

Now In this particular case we were having a function in side LINQ to Entites, which was unable to be translated to SQL Query and hence the Exception was thrown. SO by removing the function it work perfectly.

return ( from m in objDB.Modules orderby m.ID select new { ID = m.ID, Name = m.Name, Status = m.Status == true ? 1 : 0 } );

Community
  • 1
  • 1
Dreamweaver
  • 1,328
  • 11
  • 21
  • While this may answer the question it would be helpful for the asker if you included comments in your code and/or include an additional explanation instead of providing a code-only answer. – JNYRanger Jun 11 '15 at 15:03
  • Additional explanation would improve your answer. – ryanyuyu Jun 11 '15 at 15:16
  • Yea sure will keep in mind.. next time onwards.. currently editing the answer with comment:) – Dreamweaver Jun 11 '15 at 18:51
-1

Just remove your function and it will work if your values is not nullable

return (
from m in objDB.Modules
orderby m.ID
select new
   {
    ID = m.ID,
    Name = m.Name,
    Status = Convert.ToInt32( m.Status )
   }
);

If it is nullable you will need to check if it has value:

return (
from m in objDB.Modules
orderby m.ID
select new
   {
    ID = m.ID,
    Name = m.Name,
    Status =m.Status.HasValue? Convert.ToInt32( m.Status ):0
   }
);
Yuri
  • 2,820
  • 4
  • 28
  • 40
  • With this, the same message is shown: **" LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' method, and this method cannot be translated into a store expression. "** That exactly why I've created the **"ToInt32( Boolean Val )"** method that I comment above. With these method it works on SQL but no with PostGre (or at least, no with the new modified EDMX that I had to created to PostGre works). Thanks anyway. – BernardoMorais Jun 11 '15 at 16:01