【问题标题】:CRM 2011: An unhandled exception of type 'System.InvalidOperationException' occurred. referenced from scope '', but it is not definedCRM 2011:发生“System.InvalidOperationException”类型的未处理异常。从范围''引用,但未定义
【发布时间】:2014-05-30 10:48:48
【问题描述】:

我在执行此查询时收到以下错误消息。如果我删除第二个 where 子句,查询就可以正常工作。

代码

(from cl in context.CreateQuery<ContractDetail>()
  join a in context.CreateQuery<Account>()
      on cl.CustomerId.Id equals a.AccountId
  where cl.StateCode.Value == 0
  where cl.new_SupportedBy == a.Name
  select cl).ToList();

错误

An unhandled exception of type 'System.InvalidOperationException' occurred in ConsoleApplication1.exe

Additional information: variable '&lt;&gt;h__TransparentIdentifier0' of type '&lt;&gt;f__AnonymousType0``2[ConsoleApplication1.ContractDetail,ConsoleApplication1.Account]' referenced from scope '', but it is not defined

更新

尝试硬编码 where 子句中的值,如下所示,并且两次都有效。不明白是什么问题。

第一次尝试为:

where cl.new_SupportedBy == "abc"

其次:

where a.Name == "abc"

Linq Pad 抛出异常

at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)
at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)
at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateExpressionToValue(Expression exp, ParameterExpression[] parameters)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateExpressionToConditionValue(Expression exp, ParameterExpression[] parameters)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereCondition(BinaryExpression be, FilterExpressionWrapper parentFilter, Func`2 getFilter, Boolean negate)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhere(String parameterName, BinaryExpression be, FilterExpressionWrapper parentFilter, Func`2 getFilter, List`1 linkLookups, Boolean negate)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereBoolean(String parameterName, Expression exp, FilterExpressionWrapper parentFilter, Func`2 getFilter, List`1 linkLookups, BinaryExpression parent, Boolean negate)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetQueryExpression(Expression expression, Boolean& throwIfSequenceIsEmpty, Boolean& throwIfSequenceNotSingle, Projection& projection, NavigationSource& source, List`1& linkLookups)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](Expression expression)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetEnumerator[TElement](Expression expression)
at Microsoft.Xrm.Sdk.Linq.Query`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at UserQuery.RunUserAuthoredQuery() in c:\Users\abc\AppData\Local\Temp\LINQPad\_nmuxfnrq\query_lnnofb.cs:line 34

提前致谢

【问题讨论】:

  • 如果你删除了第一个 where 子句?您是否也尝试颠倒 where 子句的顺序?
  • 已经试过了。尝试分别对 cl.new_suportedby 和 a.name 进行硬编码。两次都成功了。
  • 有一种简单的方法可以检查 CRM LINQ 提供程序是否有问题,在每个 CreateQuery 之后执行 ToList(),并检查它是否有效
  • 它需要很长时间了。我还在等:)
  • ToList() 速度太慢,出现异常:发生了“System.OutOfMemoryException”类型的未处理异常

标签: c# linq dynamics-crm-2011 dynamics-crm


【解决方案1】:

我将问题整理如下:

var contractLines = (from cl in context.CreateQuery<ContractDetail>()
  join a in context.CreateQuery<Account>()
  on cl.CustomerId.Id equals a.AccountId                                 
  where cl.StateCode.Value == 0
  select new {cl, a}).ToList();

var collection = new EntityCollection();

foreach (var line in contractLines)
{
    if (line.a.Name == line.cl.dbc_SupportedBy)
    {
        collection.Entities.Add(line.cl);
    }
}

我明白,这不是一个完美的解决方案。但由于这个应用程序只运行一次,所以我不介意它是否运行缓慢。但是我仍然很想知道为什么会发生错误,所以我会保持这个问题的开放性。

更新

保持开放超过六个月没有任何回复。所以我现在关闭它,因为这个解决方法对我有用,并且为了我的回答接受率而关闭:P

【讨论】:

    【解决方案2】:

    也许 cl.new_SupportedBy == a.Name 是问题所在

    您不能编写其中 where 子句包含比较属性的谓词的 CRM LINQ 查询;即使这些属性是在同一个实体实例上定义的。

    https://social.microsoft.com/Forums/en-US/2d510944-c46f-4698-a6aa-c3ed5f76dc77/freaky-linq-issues-with-joins?forum=crmdevelopment

    【讨论】:

      猜你喜欢
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多