首页 文章

转换.Int64未重新调整LINQ实体

提问于
浏览
2

我得到以下异常:LINQ to Entities无法识别方法'Int64 ToInt64(System.String)'方法,并且此方法无法转换为存储表达式 .

我有long.Parse(ProjectID.ToString()),我看到建议是使用Convert.ToInt64,但我仍然得到相同的异常

string projID = ProjectFileID.ToString();

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

2 回答

  • 0

    只需在查询外部进行转换,因此您可以将结果直接与 long 类型的变量进行比较:

    // TODO: Error handling
    long projID = Convert.ToInt64(ProjectFileID.ToString());
    
    var d = (from f in context.FileInfo
             where f.ID == projID
             select (f));
    

    另外,鉴于你在 ProjectFileID 上调用了 ToString() ,你可能只是把它转换成它,因为它看起来肯定是 int 或类似的东西 .

  • 13

    原因是它尝试在查询本身内部进行转换,而后台SQL没有该扩展的定义 . 解决方法是在查询之外进行转换(到临时变量)并将其传递给LINQ .

相关问题