Referencing Non-Scalar Variables Not Supported

点点圈 提交于 2020-01-06 14:01:24

问题


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> matchingCandidates = _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


回答1:


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.



来源:https://stackoverflow.com/questions/6674235/referencing-non-scalar-variables-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!