【问题标题】:System.NotSupportedException: Unable to create a constant value of type_.Only primitive and...this contextSystem.NotSupportedException:无法创建类型_的常量值。只有原始和...此上下文
【发布时间】:2016-05-16 10:00:53
【问题描述】:

这似乎是编译以下 linq 查询时出现的错误。我读过一些类似的问题,但都强调使用 .Any() 函数而不是 .Contains() .. 然而,尽管如此,编译语句似乎出错并记录了异常。

这是声明:-

int result=0;
result = (from u in objContext.CLG_challengeMaster
          where u.appId == applicationId
          && objContext.CLG_taskDetail.Any(e => e.challengeId == u.id)
          && !objContext.CLG_challengeDetail.Any(e => e.challengeId == u.id && (e.userId == null && e.groupId == null && e.appGroupId == null && e.roleId == null))
          select u).Count();

这是错误日志:

Message: System.NotSupportedException: Unable to create a constant value of type 'DataAccess.Entities.Models.CLG_taskDetail'. Only primitive types or enumeration types are supported in this context.
   at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.<>c__DisplayClass0.<TypedTranslate>b__1(Expression e)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate(IEnumerable`1 argument, String argumentName, Int32 expectedElementCount, Boolean allowEmpty, Func`3 map, Func`2 collect, Func`3 deriveName)
   at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate().........

谁能帮帮忙。

【问题讨论】:

  • 最后不得不拆散并重建它:(

标签: c# sql-server linq exception


【解决方案1】:

如果&amp;&amp; objContext.CLG_taskDetail.Any(e =&gt; e.challengeId == u.id) CLG_taskDetail 可能为空,则在此行中将行更改为:

&& (objContext.CLG_taskDetail.Any() && objContext.CLG_taskDetail.Where(e => e.challengeId == u.id)) 

否则将其更改为使用“Where”子句

【讨论】:

  • 不,它不起作用。编译错误说:严重代码描述项目文件行错误CS0019运算符'&&'不能应用于'bool'和'IQueryable'类型的操作数
  • 您可以尝试以下方法,因为我认为上面的问题是您尚未加入表格。 objContext.CLG_challengeMaster.Where(u=&gt;u.appId == applicationId &amp;&amp; u.CLG_taskDetail.Any(e=&gt; e.ChallengeId == u.id) &amp;&amp; !u.CLG_challengeDetail.Any(f=&gt;f.challengeid == u.id &amp;&amp; f.userId == null &amp;&amp; f.groupId == null &amp;&amp; f.appGroupId == null &amp;&amp; f.roleId == null)).count();不认为你需要在这个上选择
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多