2

我对使用实体框架的导航属性总和有疑问

这是我的示例类

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

这很好用,但是当我有数百条记录时,速度很慢

我怎样才能让它更快但又不会失去功能?

谢谢

4

1 回答 1

2

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

于 2013-04-19T00:21:46.093 回答