引用不支持的非标量变量 [英] Referencing Non-Scalar Variables Not Supported

查看:130
本文介绍了引用不支持的非标量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我的查询错误了?我得到一个NotSupportedException无法创建JobLanguage类型的常量值。在这种情况下,只支持原始类型(如Int32,String和Guid)。

  public IQueryable< Candidate> ; GetMatchingCandidates(Job job)
{
从_db.Candidates中的候选人返回
其中,候选人.CandidateLanguages.Any(c => job.JobLanguages.Select(jl => jl.LanguageId .Value).Contains(c.LanguageId))
orderby candidate.Name降序
选择候选人
}

//调用者
列表<候选> ; matchingCandidates = _repository.GetMatchingCandidates(job).ToList();

显然,这是一个已知的问题( http://msdn.microsoft.com/en-us/library /bb896317.aspx#RefNonScalarClosures ),但我想知道如何解决它,基本上,我想要做的是这样的:使用linq将两个列表比较为sql

解决方案

嗯,你可以尝试一种方法是提取一组所需的语言ID:



(我假设语言ID是字符串如果没有,请提供更多信息。)

  public IQueryable< Candidate> GetMatchingCandidates(Job job)
{
列表< string> languageIds = job.JobLanguages
.Select(jl => jl.LanguageId.Value)
.ToList();
从_db.Candidates中的候选人返回
其中candidate.CandidateLanguages
.Any(languageIds.Contains(c.LanguageId))
orderby candidate.Name降序
选择候选;
}

如果作业已经在数据库中,您可以尝试执行内部查询使用作业ID来引用数据库的副本而不是本地数据库。


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

解决方案

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.

这篇关于引用不支持的非标量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆