开发者

Referencing Non-Scalar Variables Not Supported

hat is wrong with my query below? I'm getting a NotSupportedException ""Unable to create a constant value of type JobLanguage. Only primitive types ('such as Int32, String, and Guid') are supported in this context."

public IQueryable<Candidate> GetMatchingCandidates(Job job)
{
   return from candidate in _db.Candidates                       
   where candidate.CandidateLanguages.Any(c => job.JobLanguages.Select(jl =>      jl.LanguageId.Value).Contains(c.LanguageId)) 
   orderby candidate.Name descending
   select candidate;                            
}

//caller 
List<Candidate> matchin开发者_如何学CgCandidates = _repository.GetMatchingCandidates(job).ToList();

Apparently, this is a known issue (http://msdn.microsoft.com/en-us/library/bb896317.aspx#RefNonScalarClosures) but I'm wondering how I can get around it. Basically, what I'm trying to do is this: Comparing two lists using linq to sql


Well, one thing you could try is extracting the set of desired language IDs to start with:

(I'm assuming language IDs are strings. If they're not, please give more information.)

public IQueryable<Candidate> GetMatchingCandidates(Job job)
{
    List<string> languageIds = job.JobLanguages
                                  .Select(jl => jl.LanguageId.Value)
                                  .ToList();
   return from candidate in _db.Candidates                       
          where candidate.CandidateLanguages
                         .Any(languageIds.Contains(c.LanguageId)) 
          orderby candidate.Name descending
          select candidate;                            
}

If the job is already in the database, you could try performing an inner query using the job ID to refer to the database copy of the data rather than the local one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜