Mikkel Nielsen
Mikkel Nielsen

Reputation: 792

Linq: where EntityCollection.Contains() not working

Im having a littel problem with LINQ

I have a EntityCollection of "Resource" Each "Resource" have a EntityCollection of "Applications"

I now need to find the "Rresources" for a particular "Application"

I first find the given application:

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

This gives me the "Application"

I then try to find the applications for this "Application":

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

This is accepted in VS, but when i run it debug the "result view" returnes with a

"System.SystemException = {"Unable to create a constant value of type 'Application_Configuration_Management.Models.Application'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."}"

Can anyone help me?

Upvotes: 1

Views: 993

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726699

Your LINQ2SQL or EF provider complains that it cannot translate a query that uses an application in a context of an IN list. Replacing a search for an application followed by a search for one of its resources with a single search for a resource by application ID may work better in your situation:

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

Upvotes: 2

Related Questions