Linq to SQL gives NotSupportedException when using local variables

Posted by zwanz0r on Stack Overflow See other posts from Stack Overflow or by zwanz0r
Published on 2010-03-11T13:52:46Z Indexed on 2010/03/11 19:49 UTC
Read the original article Hit count: 269

Filed under:
|

It appears to me that it matters whether you use a variable to temporary store an IQueryable or not. See the simplified example below:

This works:

List<string> jobNames = new List<string> { "ICT" };
var ictPeops = from p in dataContext.Persons
               where (  from j in dataContext.Jobs
                        where jobNames.Contains(j.Name)
                        select j.ID).Contains(p.JobID)
               select p;

But when I use a variable to temporary store the subquery I get an exception:

List<string> jobNames = new List<string> { "ICT" };
var jobs = from j in dataContext.Jobs
           where jobNames.Contains(j.Name)
           select j.ID;
var ictPeops = from p in dataContext.Persons
               where jobs.Contains(p.JobID)
               select p;

"System.NotSupportedException: Queries with local collections are not supported"

I don't see what the problem is. Isn't this logic that is supposed to work in LINQ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql