【问题标题】:Specific linq exception when converting string to int. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' m将字符串转换为 int 时的特定 linq 异常。 LINQ to Entities 无法识别方法 'Int32 ToInt32(System.Object)' m
【发布时间】:2013-06-05 10:10:28
【问题描述】:

我遇到以下异常。我检查了设计器,类和机会代码是一个 int。

LINQ to Entities 无法识别方法 'Int32 ToInt32(System.Object)' 方法,并且该方法无法转换为存储表达式

public tblOpportunity GetOpportunityByCode(string clientCode, string opportunityCode)
        {
            tblOpportunity opportunity = null;

            ConnectionHandler.Invoke<EntityConnection>((connection) =>
            {
                var context = new xxEntities();
                opportunity = context.tblOpportunities.FirstOrDefault<tblOpportunity>(o => o.ClientCode == clientCode && o.OpportunityCode == Convert.ToInt32(opportunityCode));
            });

            return opportunity;
        }
    }

public partial class tblOpportunity
    {

        public int OpportunityCode { get; set; }

【问题讨论】:

  • 你试过o.OpportunityCode.ToString() == opportunityCode吗?
  • 你确定它是 int 类型,因为它可以是 int 吗? (nullable int) 或者您可以使用 automapper 将 linq 查询的输出映射到您的领域特定模型,这将减少手动映射的痛苦
  • 使用这个-> o.OpportunityCode == (int)opportunityCode 而不是 o.OpportunityCode == Convert.ToInt32(opportunityCode)

标签: c# linq entity-framework


【解决方案1】:
 public tblOpportunity GetOpportunityByCode(string clientCode, string opportunityCode)
    {
        tblOpportunity opportunity = null;
        var convertedOpportunityCode = Convert.ToInt32(opportunityCode);
        ConnectionHandler.Invoke<EntityConnection>((connection) =>
        {
            var context = new DMSEntities();
            opportunity = context.tblOpportunities.FirstOrDefault<tblOpportunity>(o => o.ClientCode == clientCode && o.OpportunityCode == convertedOpportunityCode);
        });

        return opportunity;
    }

这应该可以解决问题。您的问题是实体框架无法将您的表达式转换为有效的 sql,因为 sql 中不存在 Convert.ToInt32 之类的东西。

【讨论】:

    【解决方案2】:

    您可以通过先执行转换然后查询数据库来轻松解决此问题:

    public tblOpportunity GetOpportunityByCode(
                              string clientCode, string opportunityCode)
    {
        tblOpportunity opportunity = null;
    
        var convertedOpportunityCode = Convert.ToInt32(opportunityCode);
    
        ConnectionHandler.Invoke<EntityConnection>((connection) =>
        {
            var context = new xxEntities();
            opportunity = context.tblOpportunities
                                 .FirstOrDefault(o =>
                                     o.ClientCode == clientCode &&
                                     o.OpportunityCode == convertedOpportunityCode);
         });
    
         return opportunity;
     }
    

    【讨论】:

      【解决方案3】:

      LINQ 告诉您的是,它没有实现将ToInt32 功能推送到后端的功能。但是,您可以在自己的代码中毫无问题地执行此操作:

      public tblOpportunity GetOpportunityByCode(string clientCode, string opportunityCode) {
          tblOpportunity opportunity = null;
          // Do the conversion outside LINQ
          var opCodeInt = Convert.ToInt32(opportunityCode);
          ConnectionHandler.Invoke<EntityConnection>((connection) => {
              var context = new xxEntities();
              opportunity = context.tblOpportunities.FirstOrDefault<tblOpportunity>(
                  o => o.ClientCode == clientCode && o.OpportunityCode == opCodeInt
              ); //                                                       ^^^^^^^^^
          });
          return opportunity;
      }
      

      【讨论】:

        【解决方案4】:

        该方法在表达式中不起作用,因为它不能直接翻译成后备存储查询语言,但在此之前您可以很好地进行转换;先验地从字符串解析为整数,然后在查询中正确使用本地定义的int

        这样做时,我个人应该使用int.TryParse 而不是Convert.ToInt32,这样您就可以更恰当地处理无效输入,而不仅仅是将结果放入表达式中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-09
          • 1970-01-01
          • 1970-01-01
          • 2016-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多