entity-framework - Entity Framework 中相关属性的求和

标签 entity-framework aggregate-functions

我在使用 Entity Framework 的导航属性总和方面遇到问题

这是我的示例类

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ObservableCollection<Call> Calls { get; set; }
    [NotMapped]
    public decimal TotalCallDuration { get { return Calls.Sum(c => c.Value); } }

}

public class Call
{
    public int Id { get; set; }
    public int CustomerID { get; set; }
    public virtual Customer Customer { get; set; }
    public decimal Value { get; set; }
    public DateTime Date { get; set; }
}

这很有效,但是当我有数百条记录时,它非常慢

如何才能使其更快但又不丢失功能?

谢谢

最佳答案

你想做的是: customer.TotalCallDuration = context.Call.Sum(x => x.Value).Where(x => x.CustomerID == customer.Id);

关于entity-framework - Entity Framework 中相关属性的求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095194/

相关文章:

sql-server - 在 EF4 中使用 NOLOCK 提示?

c# - Entity Framework 添加和删除实体?

c# - 为什么代码首先要创建索引列?

sql - GROUP BY + CASE 语句

c# - 如何设置自动递增属性的初始值(DatabaseGeneratedOption.Identity)

c# - 根据 Entity Framework 中的数据库列值显示枚举文本

sql - 新手问题: N-N join that matches ALL a list

mysql - SQL 中总和的差异

mysql - 检索连接表中没有匹配行的记录

sql - 如何计算运行 SUM?