Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression.





C#
public List<SalaryMonthlyViewModel> getallemployeeSalary()
     {
         var data = (from a in objentity.TblSalaryMonthlies
                     join b in objentity.TblEmployeeRegistrations on a.EmployeeId equals b.EmployeeId

                     select new SalaryMonthlyViewModel
                     {
                         SalaryMonthlyId = a.SalaryMonthlyId,
                         Month = a.Month,
                         Year = a.Year,
                         NetSalary = Convert.ToInt32(a.NetSalary),
                         PresentDay = a.PresentDay,
                         UserName = b.UserName
                     }).ToList();
         return data;
     }
Posted
Comments
phil.o 25-Apr-14 6:41am    
What is the definition of SalaryMonthlyViewModel object, especially its NetSalary property: which type is it?
[no name] 25-Apr-14 6:50am    
namespace LaxmiTatkal.Model
{
public class SalaryViewModel
{

public int SalaryId { get; set; }
public int Salary { get; set; }

public string Username { get; set; }
}
}

Trying to modify your code hope it helps. :)

C#
public List<salarymonthlyviewmodel> getallemployeeSalary()
     {
         var data = (from a in objentity.TblSalaryMonthlies
                     join b in objentity.TblEmployeeRegistrations on a.EmployeeId equals  b.EmployeeId
 
                     select new SalaryMonthlyViewModel
                     {
                         SalaryMonthlyId = a.SalaryMonthlyId,
                         Month = a.Month,
                         Year = a.Year,
                         NetSalary = a.NetSalary,
                         PresentDay = a.PresentDay,
                         UserName = b.UserName
                     }).ToList()// now we have in-memory query
                  .Select(c => new SalaryMonthlyViewModel()
                  {
                         SalaryMonthlyId = a.SalaryMonthlyId,
                         Month = a.Month,
                         Year = a.Year,
                         NetSalary = Convert.ToInt32(a.NetSalary),//This should work
                         PresentDay = a.PresentDay,
                         UserName = b.UserName
                  });
         return data;
     }
</salarymonthlyviewmodel>
 
Share this answer
 
v3
Comments
[no name] 25-Apr-14 7:15am    
still i get error in belove line

netsalary=a.netsalary
Sanket Saxena 25-Apr-14 7:23am    
did you change the .Select(c => new CourseNames() of my solution."CourseNames()" should be change according to your code. i.e. salarymonthlyviewmodel()
[no name] 25-Apr-14 8:06am    
i copy u whole code and post in my project but still i get my error this place
Sanket Saxena 25-Apr-14 8:12am    
i update the solution try again and let me the error if any.
I think ur NetSalary in table is not of Integer type thats why ur getting that error
 
Share this answer
 
Comments
[no name] 25-Apr-14 7:09am    
SalaryMonthlyId int Unchecked
EmployeeId int Unchecked
NetSalary int Checked
Month int Unchecked
Year int Unchecked
PresentDay int Unchecked
IsActive bit Unchecked
IsDelete bit Unchecked
CreatedDate datetime Unchecked
ModifyDate datetime Checked
You should create in your SalaryMonthlyViewModel class a property of type string named NetSalaryString and use that property in your LINQ expresion above in place of NetSalary.

The new property will do the convention to int like in the next code:
C#
public class SalaryMonthlyViewModel <br />
{<br />
//...<br />
private _netSallaryString;<br />
public NetSalaryString<br />
{<br />
   get{ return _netSallaryString;}<br />
   set{<br />
      _netSallaryString = value;<br />
       this.NetSalary = Convert.ToInt32(value);<br />
   }<br />
}<br />
//...<br />
}
 
Share this answer
 
Comments
[no name] 25-Apr-14 7:15am    
i get error in all ur line
Just stay with Linq query. You need to to change the blow Line

C#
NetSalary = Convert.ToInt32(a.NetSalary),

to
C#
NetSalary = int.Parse(a.NetSalary),

This change may help you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900