【问题标题】:Find entities which has fewer than n sub-entities查找具有少于 n 个子实体的实体
【发布时间】:2012-01-03 15:18:44
【问题描述】:

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

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

我正在努力寻找目前Customers 少于MaxActiveCustomers 建议的任何代表。

我的尝试:

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.

正确的做法是什么?

【问题讨论】:

  • 此外,我认为 Count(predicate) 在 Linq 中对实体不起作用。看看here(搜索count)
  • @Reniuz Count 应该可以正常工作。

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


【解决方案1】:

您已将Customers 定义为IEnumerable&lt;Customer&gt; 类型,EF 不将其视为模型的一部分。将类型更改为ICollection&lt;Customer&gt;

【讨论】:

  • 我知道它必须非常简单。这就是我在让其他人审查我的代码之前发布到 SO 的结果...... :)
猜你喜欢
  • 2011-08-25
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多