[英]How can I sum data from a database table using Entity Framework?
In my claim table I have multiple columns. 在我的索赔表中,我有多列。 One of these columns is the total claim amount. 这些列之一是索赔总额。 How can I retrieve this data from the database and find the sum of these totals? 如何从数据库中检索此数据并找到这些总数的总和? I already know how to write a loop to find this sum but not how to retrieve the data from the database and put it in a usable format. 我已经知道如何编写一个循环来查找此总和,但不知道如何从数据库中检索数据并将其以可用格式放置。 Thank you in advance. 先感谢您。
public ActionResult About()
{
IQueryable<ClaimsDateGroup> data = from claim in db.Claims
group claim by claim.CreatedDate into dateGroup
select new ClaimsDateGroup();
return View();
}
如果您只需要索偿的总和,请使用Sum
(只需用实际的列名替换ClaimsMade
):
db.Claims.Sum(c => c.ClaimsMade)
It's still unclear what you try to achieve and what technology to use. 尚不清楚您尝试实现什么以及使用什么技术。 If you like to use Sql.Linq for this then you need to build aggregation method. 如果您想为此使用Sql.Linq,则需要构建聚合方法。 Something like 就像是
Decimal? totalOfClaims = (from claim in db.Claims
group claim by claim.CreateDate
select claim.CreateDate).Count();
You'll get total number of claims for different dates. 您将获得不同日期的索赔总数。 If this isn't what you after then provide more details in the question. 如果这不是您想要的,请在问题中提供更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.