简体   繁体   English

如何使用Entity Framework对数据库表中的数据求和?

[英]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.

相关问题 使用实体框架如何从表中检索集合? - Using Entity Framework how can I retrieve a collection from a table? 从Entity Framework 6生成数据库时,如何从表名中删除“ Set”? - How can I remove “Set” from table name, when I generate database from Entity framework 6? 如何使用 Entity Framework Core 从数据图中分离实体? - How can I detach an entity from data graph using Entity Framework Core? 如何在Entity Framework中使用介于两者之间的类从另一个实体获取数据 - How can I get data from another entity using class in between in Entity Framework 如何使用实体框架和 Oracle 托管数据访问连接到 Oracle 数据库? - How can I connect to a Oracle Database using Entity Framework and Oracle Managed Data Access? 如何使用Entity Framework为静态数据建立数据库? - How do I seed a database with static data using Entity Framework? 如何使用Entity Framework 6在运行时创建数据库和表? - How can I create a database and tables at runtime using Entity Framework 6? 如何使用Entity Framework 4.2执行数据库迁移? - How can I perform database migration using Entity Framework 4.2? 如何使用 Entity Framework 6 从数据库中获取数据 - How to Fetch Data from database using Entity Framework 6 如何从Entity Framework中的.edmx文件生成数据库? - How can I generate the database from .edmx file in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM