在linq查询asp.net中将字符串转换为十进制
问题描述:
我正在尝试使用实体查询从我的数据库表中获取一些数据的总和
i am trying to get the sum of some data from my database table using Entity Query
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; }
}
但我收到以下错误:
LINQ to Entities无法识别方法' System.Decimal Parse(System.String)
请告诉我我的代码有什么问题?
提前谢谢
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
答
LINQ to Entites从你的代码构建一个SQL语句...它也不知道如何处理该Parse部分,因为它无法转换为SQL ...
什么该列的SQL类型?你确定需要转换吗?我的直觉说这也是SQL中的一个数值,所以你把它变成.NET十进制的所有努力都是不必要的......
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...