简体   繁体   中英

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

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.

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM