Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities does not recognize the method and this method cannot be translated into a store expression [duplicate]

var ps = dbContext.SplLedgers.Select(p => new SplLedgerModel
            {
                Name = p.Name,
                VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType))
        });

I am getting the following exception, whats wrong with the code.

JIMS.VoucherType is an enum

+       $exception  {"LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method, and this method cannot be translated into a store expression."}    System.Exception {System.NotSupportedException}
like image 352
Kishore Kumar Avatar asked Sep 28 '12 14:09

Kishore Kumar


1 Answers

Your code is basically trying to find the Convert.ToString() method in the DB and understandably failing.

You can get around it, for example by making sure the query executes before the select, e.g.

var ps = dbContext.SplLedgers.Select(p => new  
    { 
         Name = p.Name, 
         VoucherType = p.VoucherType
    }).ToList().Select(p => new SplLedgerModel( 
    { 
         Name = p.Name, 
         VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType)) 
    }); 
like image 177
Justin Harvey Avatar answered Sep 27 '22 22:09

Justin Harvey