实体框架中的相关属性

时间:2013-04-19 00:16:52

标签: entity-framework aggregate-functions

我使用实体框架

的导航属性总和存在问题

这是我的示例类

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; }
}

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

如何让它更快但不失功能?

谢谢

1 个答案:

答案 0 :(得分:2)

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