查找少于n个子实体的实体

时间:2012-01-03 15:18:44

标签: c# linq entity-framework ef-code-first

我正在尝试找到一种简单的方法来挑选一位客户服务代表来分配给定用户。我有以下型号:

public class Customer
{
    public int Id { get; set; }
    // SNIP
    public virtual Representative Representative { get; set; }
    public bool Active { get; set; }
}

public class Representative
{
    public int Id { get; set; }
    public int MaxActiveCustomers { get; set; }
    // all the customers this representative has interacted with
    public IEnumerable<Customer> Customers { get; set; }
}

我正在尝试找到目前CustomersMaxActiveCustomers更少的代表。

我的尝试:

from r in Representatives
where r.MaxActiveCustomers > r.Customers.Count(c => c.Active)
select r

这给了我以下例外:

NotSupportedException: The specified type member 'Customers' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:5)

您已将Customers定义为类型IEnumerable<Customer>,EF不会将其视为模型的一部分。将类型更改为ICollection<Customer>