Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to get the sum of some data from my database table using Entity Query

C#
public List<total> gettotal(int apid)
    {
        var query = (from a in DB.TblApplicantInstallments
                     where
                         a.ApplicantId == apid
                     group a by a.ApplicantId into g
                     select new total()
                         {
                             paidtotal = g.Where(x => x.Status == "Paid").Sum(x =>decimal.Parse(x.InstallmentDueAmount)),
                            unpaidtoal = g.Where(x => x.Status == "Not Paid").Sum(x => (decimal)x.InstallmentDueAmount)

                         }).ToList();
        List<total> list = query.ToList();
        return list;
           }
}
public class total
{
    public decimal? paidtotal { get; set; }
    public decimal? unpaidtoal { get; set; }
}

but i am getting the following error:
LINQ to Entities does not recognize the method 'System.Decimal Parse(System.String)
please tell me what is wrong with my code?
Thanks in advance
Posted
Updated 14-Sep-18 16:40pm
Comments
Tomas Takac 27-Jan-15 3:30am    
You cannot use any function in LINQ to entities. This gets translated to SQL and only few function calls are supported in the translation. To do such a thing you would need to materialize the enumeration on client by calling e.g. ToList() and then doing the calculation. What datatype is InstallmentDueAmount? Do you need the conversion?

1 solution

LINQ to Entites builds an SQL statement from your code...It do nor know what to do with that Parse part as it can not be converted to SQL...
What the SQL type of that column? Are you sure you need to convert it? My intuition says that is is a numeric value in SQL too, so all your effort to make it into .NET decimal is needless...
 
Share this answer
 
Comments
saifullahiit 27-Jan-15 4:37am    
it is nvarchar value. i want to convert it to decimal
Kornfeld Eliyahu Peter 27-Jan-15 4:41am    
It sound to me as a design problem that you store amount as string...However try to write your line like this:
paidtotal = g.Where(x => x.Status == "Paid").Sum(x =>(decimal)x.InstallmentDueAmount)
saifullahiit 27-Jan-15 4:43am    
have tried it before it says cant convert string to decimal.
Kornfeld Eliyahu Peter 27-Jan-15 4:45am    
What the value of the string?
saifullahiit 27-Jan-15 4:46am    
installmentdueAmount is saving cost(10000,2000,3000) and its datatype is nvarchar

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