Linq to Dynamics - 比较 where 子句中的两个属性

标签 linq dynamics-crm dynamics-crm-2011

我正在使用 Dynamics sdk dll 的版本 5.0.9689.2165 并尝试使用 Linq 获取所有 Account.XDate 小于 Account.YDate 的帐户(两者都是自定义 DateTime 属性 - 我使用生成的代理类在项目中访问这些)从 Dynamics Online 帐户。

我有这个基本的表达方式:

var accounts = myOrganizationServiceContext.CreateQuery<Account>().Where(a => a.XDate < a.YDate)

但是当它被处理时我得到下面的异常 - 你不能比较服务器上的 2 个实体属性吗?

System.InvalidOperationException occurred
  Message=variable 'a' of type 'Account' referenced from scope '', but it is not defined
  Source=System.Core
  StackTrace:
       at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
       at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
       at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression 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.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
       at Microsoft.Xrm.Sdk.Linq.QueryProvider.CompileExpression(LambdaExpression expression)
       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.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.TranslateWhere(QueryExpression qe, String parameterName, Expression exp, List`1 linkLookups)
       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)

我的电话就在这里

内部异常:

最佳答案

这代表了 CRM Linq 提供商的限制(又一次)。 Per Microsoft :

where
The left side of the clause must be an attribute name and the right side of the clause must be a value. You cannot set the left side to a constant. Both the sides of the clause cannot be constants.

要使用 Linq 提供程序解决您的特定问题,您可能必须在内存中收集整个 AccountSet,然后对结果使用常规 Linq 方法以获得您想要的最终结果。

var accounts = myOrganizationServiceContext
    .CreateQuery<Account>()
    .Select(a => new { a.AccountId, a.XDate, a.YDate })
    .ToList();

var filteredAccounts = accounts
    .Where(a => a.XDate < a.YDate);

关于Linq to Dynamics - 比较 where 子句中的两个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11874133/

相关文章:

LinqKit PredicateBuilder 与 EF 4 CPT 5 表关系?

internet-explorer - 如何获取对嵌入在另一个应用程序中的 Internet Explorer 实例的 COM 引用

sql-server - 根据 CRM View 获取任何实体记录的审核历史记录

javascript - 在 CRM 2011 的 Web 资源中出现错误对象不支持属性或方法 'setSrc'

dynamics-crm-2011 - crm webapi 中的多级扩展

javascript - crm2011,javascript onchange 事件系统实体

dynamics-crm-2011 - 使用RetrieveMultipleRequest检索每个实体的相关实体

c# - LINQ 到 SQL : GroupBy() and Max() to get the object with latest date

c# - Linq 结果内的嵌套连接数据

c# - 将两种方法重构为一种