Linq to Dynamics-比较where子句中的两个属性 [英] Linq to Dynamics - compare two properties in where clause

查看:93
本文介绍了Linq to Dynamics-比较where子句中的两个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Dynamics sdk dll's ver 5.0.9689.2165 and trying to use Linq to get all Accounts where the Account.XDate is less than the Account.YDate (both are custom DateTime properties - I use the genenerated proxy classes in project to access these) from a Dynamics Online account.

我有这个基本表达:

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

但是我在处理它时遇到了下面的异常-您不能在服务器上比较2个Entity属性吗?

But I get the exception below when it gets processed - can't you compare 2 Entity properties up on the server?

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)

我的电话在这里

InnerException:

InnerException:

推荐答案

这表示CRM Linq提供程序的局限性(再次). 根据Microsoft :

This represents a limitation of the CRM Linq provider (yet again). 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方法以获得最终结果.

To solve your particular problem using the Linq provider, you might have to gather the entire AccountSet in memory, then use regular Linq methods against the result to get the final result you're after.

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子句中的两个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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