无法创建常量值...此上下文仅支持基本类型或枚举类型

时间:2017-01-19 20:22:07

标签: c# linq visual-studio-2015

错误:

我收到上述错误,以下是我的堆栈跟踪:

  

无法创建类型的常量值   ' JackHenry.JHAKeys.Model.ADSENTDB.L_FORMS_SEL_APPLICANT&#39 ;.只要   在此上下文中支持原始类型或枚举类型。

     

来源:" mscorlib"

     

在System.Threading.Tasks.Task.WaitAll(Task []任务,Int32   millisecondsTimeout,CancellationToken cancellationToken)at   System.Threading.Tasks.Task.WaitAll(Task []任务,Int32   在System.Threading.Tasks.Task.WaitAll(Task []的millisecondsTimeout)   任务)   Keys.BusinessLogic.Lending.Document.Fill.LoanFillService.FillLoanObject(字符串   workID,Boolean documentSelection)in   F:\ JHA6 \ JHAKEYS \ 2018 \ SERVICES \ DEV \ ArgoServices \ Keys.BusinessLogic \贷款\文档\填充\ LoanFillService.cs:行   422

我已将问题缩小到以下linq语句:

var query = from t in ctx.L_FORMS_SELECTION
            where (ctx.L_FORMS_SEL_APPLICANT.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_APP_INSIDER.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_PROD_CODE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_PROD.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_QUEUE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_COLLAT.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_COLLAT_STATE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_COLLAT_VIEW.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_LOAN.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_LOAN_JURISDICTION.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_LOAN_PURPOSE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_REG.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_SUBSTITUTE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_CREDIT_INS.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_APP_MARITAL_STAT.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_APP_STATE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_RATE.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_COLLAT_COUNTY.Any(c => (c.form_id == t.form_id)) ||
            ctx.L_FORMS_SEL_LOAN_APPSOURCE.Any(c => (c.form_id == t.form_id)))
            && t.shared_service_ind.ToUpper().Trim() == "E"
            select t;

if ((query.FirstOrDefault() != null))
                    return true;

显然只是试图搜索一堆表来查看其中任何一个值是否存在。

背景

我理解它正在寻找原语(即guid),但我不确定如何将其用于有效的linq语句。我搜索了 SO ,发现很多文章讨论了类似的问题。但是,我要么完全不了解问题,要么就我的问题如何正确解释这些解决方案。

VS 2013中运行时,此查询正常运行。问题仅发生在VS 2015。我使用 .Net framework 4 Entity Framework 5 (版本4.4)。我使用的是ReSharper的许可证,但此后又将其关闭了。我不确定我是否错过了配置或未正确同步。我当然看起来似乎很好......不确定为什么visual studio 2015正在表现?

我还重新创建了工作区并使用VS 2015进行了重建,以确保VS 2013无法在某处缓存某些内容。

试验

我已将问题简化为仅2个表,以下查询也给出了此错误:

var tmp2 = from t in ctx.L_FORMS_SELECTION
           where ctx.L_FORMS_SEL_APPLICANT.Any(c => (c.form_id == t.form_id))
           select t;
if ((tmp2.FirstOrDefault() != null))
    return true;

经过一些简化后,我最终得到了一个有效的查询。这个包含上面的两个表格以及第三个用于说明的表格:

var tmp3 = from frmSel in ctx.L_FORMS_SELECTION
           join frmApp in ctx.L_FORMS_SEL_APPLICANT on frmSel.form_id equals frmApp.form_id
           join ins in ctx.L_FORMS_SEL_APP_INSIDER on frmSel.form_id equals ins.form_id
           select t3;
if ((tmp3.FirstOrDefault() != null))
    return true;

所以这更好,但我需要结合20个表...而不仅仅是3.我可以做很多连接,但这种方法效率会降低并产生更多开销吗?另外,我仍然不确定为什么原始查询在VS 2013中有效但在VS 2015中无效 - 也许我只需要关注它而不是花费精力重构查询?

由于查询没有爆炸,我对其进行了扩展,只是重新创建了我的原始查询,但这一次使用了' join'关键字:

var tmp4 = from t in ctx.L_FORMS_SELECTION
           join app in ctx.L_FORMS_SEL_APPLICANT on t.form_id equals app.form_id
           join appState in ctx.L_FORMS_SEL_APP_STATE on t.form_id equals appState.form_id
           join appSource in ctx.L_FORMS_SEL_LOAN_APPSOURCE on t.form_id equals appSource.form_id
           join appMarital in ctx.L_FORMS_SEL_APP_MARITAL_STAT on t.form_id equals appMarital.form_id
           join appIns in ctx.L_FORMS_SEL_APP_INSIDER on t3.form_id equals appIns.form_id
           join prodCode in ctx.L_FORMS_SEL_PROD_CODE on t.form_id equals prodCode.form_id
           join prod in ctx.L_FORMS_SEL_PROD on t.form_id equals prod.form_id
           join queue in ctx.L_FORMS_SEL_QUEUE on t.form_id equals queue.form_id
           join coll in ctx.L_FORMS_SEL_COLLAT on t3.form_id equals coll.form_id
           join collState in ctx.L_FORMS_SEL_COLLAT_STATE on t.form_id equals collState.form_id
           join collView in ctx.L_FORMS_SEL_COLLAT_VIEW on t.form_id equals collView.form_id
           join collCnty in ctx.L_FORMS_SEL_COLLAT_COUNTY on t.form_id equals collCnty.form_id
           join loan in ctx.L_FORMS_SEL_LOAN on t.form_id equals loan.form_id
           join loanJur in ctx.L_FORMS_SEL_LOAN_JURISDICTION on t.form_id equals loanJur.form_id
           join loanPur in ctx.L_FORMS_SEL_LOAN_PURPOSE on t.form_id equals loanPur.form_id
           join reg in ctx.L_FORMS_SEL_REG on t.form_id equals reg.form_id
           join sub in ctx.L_FORMS_SEL_SUBSTITUTE on t.form_id equals sub.form_id
           join credit in ctx.L_FORMS_SEL_CREDIT_INS on t.form_id equals credit.form_id
           join rate in ctx.L_FORMS_SEL_RATE on t.form_id equals rate.form_id
           where t.shared_service_ind.ToUpper().Trim() == "E"
           select t;

这个不会抛出错误......但也不会返回任何值(在VS 2013中)。原始的linq查询返回了一个值(至少当我在VS 2013中运行它时) - 奇怪。

我查看了原始查询的结果(在VS2013中运行),发现有一个匹配的form_id。然后我运行了一个foreach查询,当然得到了一个匹配。似乎出于某种原因,我的平等等等。无法正常工作 - 也许这是因为form_id是Guid。

经过更多的发展......这就是我的立场。此查询只是遍历并在找到值后立即退出循环。因此,我认为这更有效率,但也有点罗嗦而不像我想的那样整洁:

foreach (var rec in ctx.L_FORMS_SELECTION)
{
    var app = ctx.L_FORMS_SEL_APPLICANT.Where(c => c.form_id == rec.form_id);
     if (app.FirstOrDefault() != null) return true;

     var appState = ctx.L_FORMS_SEL_APP_STATE.Where(c => c.form_id == rec.form_id);
     if (appState.FirstOrDefault() != null) return true;

     var appSource = ctx.L_FORMS_SEL_LOAN_APPSOURCE.Where(c => c.form_id == rec.form_id);
     if (appSource.FirstOrDefault() != null) return true;

     var appMarital = ctx.L_FORMS_SEL_APP_MARITAL_STAT.Where(c => c.form_id == rec.form_id);
     if (appMarital.FirstOrDefault() != null) return true;

     var appIns = ctx.L_FORMS_SEL_APP_INSIDER.Where(c => c.form_id == rec.form_id);
     if (appIns.FirstOrDefault() != null) return true;

     var prodCode = ctx.L_FORMS_SEL_PROD_CODE.Where(c => c.form_id == rec.form_id);
     if (prodCode.FirstOrDefault() != null) return true;

     var prod = ctx.L_FORMS_SEL_PROD.Where(c => c.form_id == rec.form_id);
     if (prod.FirstOrDefault() != null) return true;

     var queue = ctx.L_FORMS_SEL_QUEUE.Where(c => c.form_id == rec.form_id);
     if (queue.FirstOrDefault() != null) return true;

     var coll = ctx.L_FORMS_SEL_COLLAT.Where(c => c.form_id == rec.form_id);
     if (coll.FirstOrDefault() != null) return true;

     var collState = ctx.L_FORMS_SEL_COLLAT_STATE.Where(c => c.form_id == rec.form_id);
     if (collState.FirstOrDefault() != null) return true;

     var collView = ctx.L_FORMS_SEL_COLLAT_VIEW.Where(c => c.form_id == rec.form_id);
     if (collView.FirstOrDefault() != null) return true;

     var collCnty = ctx.L_FORMS_SEL_COLLAT_COUNTY.Where(c => c.form_id == rec.form_id);
     if (collCnty.FirstOrDefault() != null) return true;

     var loan = ctx.L_FORMS_SEL_LOAN.Where(c => c.form_id == rec.form_id);
     if (loan.FirstOrDefault() != null) return true;

     var loanJur = ctx.L_FORMS_SEL_LOAN_JURISDICTION.Where(c => c.form_id == rec.form_id);
     if (loanJur.FirstOrDefault() != null) return true;

     var loanPur = ctx.L_FORMS_SEL_LOAN_PURPOSE.Where(c => c.form_id == rec.form_id);
     if (loanPur.FirstOrDefault() != null) return true;

     var reg = ctx.L_FORMS_SEL_REG.Where(c => c.form_id == rec.form_id);
     if (reg.FirstOrDefault() != null) return true;

     var sub = ctx.L_FORMS_SEL_SUBSTITUTE.Where(c => c.form_id == rec.form_id);
     if (sub.FirstOrDefault() != null) return true;

     var credit = ctx.L_FORMS_SEL_CREDIT_INS.Where(c => c.form_id == rec.form_id);
     if (credit.FirstOrDefault() != null) return true;

     var rate = ctx.L_FORMS_SEL_RATE.Where(c => c.form_id == rec.form_id);
     if (rate.FirstOrDefault() != null) return true;
}

不确定这是否可以更加合理地完成 - 它的代码比原始代码更多。我希望这就是VS 2015并不喜欢=) 感谢您阅读本文,我感谢您提供的任何指导。

0 个答案:

没有答案