Luis Valencia
Luis Valencia

Reputation: 33998

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

Upvotes: 0

Views: 719

Answers (4)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

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

Upvotes: 1

b_meyer
b_meyer

Reputation: 604

 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.

Upvotes: 4

Grant Thomas
Grant Thomas

Reputation: 45083

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.

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

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

Upvotes: 1

Related Questions