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

Veg*_*sen 0 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; }
}
Run Code Online (Sandbox Code Playgroud)

我试图找到目前CustomersMaxActiveCustomers建议少的代表.

我的尝试:

from r in Representatives
where r.MaxActiveCustomers > r.Customers.Count(c => c.Active)
select r
Run Code Online (Sandbox Code Playgroud)

这给了我以下例外:

NotSupportedException: The specified type member 'Customers' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Run Code Online (Sandbox Code Playgroud)

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

Era*_*nga 5

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