简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM