【问题标题】:LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method,LINQ to Entities 无法识别方法“Int32 Parse(System.String)”方法,
【发布时间】:2015-07-20 02:32:28
【问题描述】:

我正在使用实体框架,我有一行代码将字符串字段 (id) 转换为 int 并与数字进行比较

 students = students.Where(s => (Int32.Parse( s.id)>5555));

每当我尝试运行它时,我都会收到 rhis 错误。 “LINQ to Entities 无法识别方法 'Int32 Parse(System.String)' 方法,并且该方法无法转换为存储表达式。”

我尝试了几种不同的方法,但没有任何效果,所以任何帮助都会很棒。

【问题讨论】:

  • 你试过Convert.ToInt32 吗?
  • 是的。它没有工作。并导致了那个错误
  • 试试这个Int32.TryParse
  • 如果您的model.id 有db 类型int,为什么还要在linq2entities 中将其解析为int?我认为你应该修复你的模型而不是铸造

标签: c# linq entity-framework int int32


【解决方案1】:

首先,我强烈建议不要将列转换为 int,否则会丢失列上的索引。您宁愿将 int 转换为字符串。然而,这里是如何修复您的代码。

  1. 首先签署Contributor License Agreement
  2. 那你fork the Entity Framework git repo
  3. 写一个新的MethodCallTranslator.CallTranslator,将Convert.ToInt32(string)替换为(int) string
  4. MethodCallTranslator 中注册新的MethodCallTranslator.CallTranslator
  5. 为您的测试用例编写单元测试。
  6. 入住
  7. 创建拉取请求
  8. 等等
  9. 从 nuget 下载新版本的实体框架

【讨论】:

  • 很棒的分步教程,+1
猜你喜欢
  • 2015-09-20
  • 2011-12-07
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多