Linq:其中EntityCollection.Contains()不起作用

时间:2012-08-28 12:54:01

标签: linq c#-4.0

我对LINQ

有一个小问题

我有一个EntityCollection“Resource”每个“Resource”都有一个EntityCollection“Applications”

我现在需要找到特定“应用程序”的“Rresources”

我首先找到给定的应用程序:

Application application = (from app in db.ApplicationSet
                           where app.Id == appId
                           select app).First();

这给了我“应用程序”

然后我尝试找到这个“应用程序”的应用程序:

var res = from rs in db.ResourceSet
          where rs.Applications.Contains(application)
          select rs;

这在VS中被接受,但是当我运行它时,使用

调试“结果视图”返回

“System.SystemException = {”无法创建类型为'Application_Configuration_Management.Models.Application'的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')。“}”

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

您的LINQ2SQL或EF提供商抱怨它无法在application列表的上下文中翻译使用IN的查询。更换搜索应用程序,然后通过按应用程序ID单次搜索资源来搜索其中一个资源,可能会在您的情况下更好地工作:

var res = from rs in db.ResourceSet
    where rs.Applications.Any(a => a.Id == appId)
    select rs;