Convert.Int64 Is Not Reconized LINQ To Entities

狂风中的少年 提交于 2021-01-27 20:11:06

问题


I am getting the following exception: LINQ to Entities does not recognize the method 'Int64 ToInt64(System.String)' method, and this method cannot be translated into a store expression.

I had long.Parse(ProjectID.ToString()) and I see the suggestion was to use Convert.ToInt64 but I am still getting the same exception

string projID = ProjectFileID.ToString();

            var d = (from f in context.FileInfo
                     where f.ID == Convert.ToInt64(projID)
                     select (f));

回答1:


Just do the conversion outside the query, so you compare the results directly to a variable of type long:

// TODO: Error handling
long projID = Convert.ToInt64(ProjectFileID.ToString());

var d = (from f in context.FileInfo
         where f.ID == projID
         select (f));

Also, given that you're calling ToString() on ProjectFileID, can you maybe just cast it instead, since it certainly seems like it's an int or something along those lines.




回答2:


The reason is that it tries to do the conversion inside the query itself and the behind the scenes SQL has no definition for that Extension. The workaround is doing the conversion to long outside of the query (to a temp variable) and passing that in to the LINQ.



来源:https://stackoverflow.com/questions/10095276/convert-int64-is-not-reconized-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!