2012-01-03 70 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

相關問題