2012-01-03 46 views
0

我试图找到一种简单的方法来挑选出一位客户服务代表来分配给给定的用户。我有以下型号:查找具有少于n个子实体的实体

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. 

什么是做了正确的方法是什么?

+0

除此之外,我认为计数(谓词)不会在Linq中对实体起作用。看看[这里](http://msdn.microsoft.com/en-us/library/bb738550.aspx)(搜索计数) – Reniuz 2012-01-03 15:29:00

+0

@Reniuz伯爵应该工作得很好。 – 2012-01-03 17:08:30

回答

5

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

+0

我知道它必须非常简单。这就是我在通过其他人审阅我的代码之前发布到SO的原因...... :) – 2012-01-04 07:50:11

相关问题