简体   繁体   English

将字符串转换为int时,特定的linq异常。 LINQ to Entities无法识别方法'Int32 ToInt32(System.Object)'m

[英]Specific linq exception when converting string to int. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' m

I am having the following exception. 我有以下例外。 I checked the designer and the class and opportunitycode is an int. 我检查了设计师,并且班级和商机代码是一个整数。

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression 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; }
 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;
    }

That should do the trick. 这应该够了吧。 You problem is that entity framework can not convert your expression into valid sql due to the fact that something like Convert.ToInt32 does not exist in sql. 您的问题是,由于sql中不存在类似Convert.ToInt32之类的事实,因此实体框架无法将您的表达式转换为有效的sql。

You can easily fix this by first performing the conversion and then querying the database: 您可以通过先执行转换然后查询数据库来轻松解决此问题:

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;
 }

What LINQ is telling you is that it does not implement functionality that pushes the ToInt32 functionality to the backend. LINQ告诉您的是,它没有实现将ToInt32功能推到后端的功能。 However, you can do it in your own code without a problem: 但是,您可以在自己的代码中完成此操作:

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;
}

The method won't work within the expression as it cannot be translated directly to the backing-store query language, but you're in good scope to do your conversions well before that; 该方法在表达式中不起作用,因为它不能直接转换为后备存储查询语言,但是您可以在此之前很好地进行转换。 do your parsing from string to integer a priori, then use the locally defined int proper in the query. 先进行从字符串到整数的解析,然后在查询中使用本地定义的int

In doing so, personally I qould use int.TryParse rather than Convert.ToInt32 so that you may handle invalid input more aptly instead of just throwing the result into the expression. 这样做时,我个人将使用int.TryParse而不是Convert.ToInt32以便您可以更适当地处理无效输入,而不仅仅是将结果扔到表达式中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.Object)&#39;方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method LINQ to Entities无法识别方法&#39;Int32 ToInt32(System.String)&#39;方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression linq to entities无法识别int32 toint32的方法 - linq to entities does not recognize the method int32 toint32 LINQ to Entities无法识别方法&#39;Int32 ToInt32(Boolean)&#39; - LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' LINQ to Entities 无法识别方法 &#39;Int32 Int32(System.String)&#39; 方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法 - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method LINQ to Entities无法识别方法&#39;System.String GetMonthName(Int32)&#39;方法 - LINQ to Entities does not recognize the method 'System.String GetMonthName(Int32)' method LINQ 到 Entities 无法识别方法 'System.String SetAssetStatus(Int32)' 方法 - LINQ to Entities does not recognize the method 'System.String SetAssetStatus(Int32)' method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM