LINQ 错误:变量 'x'类型为'y'从范围“引用”,但未定义
我遇到此错误...
从范围“”引用了类型为“Xrm.sb_directdebit paymentline”的变量“ paymentLine”,但未定义
...当我尝试执行“ paymentLines.ToList”时()' 在以下代码之后:
var payments = from payment in Crm.sb_directdebitpayments
where payment.statuscode == 1
&& Crm.sb_directdebitmandates.Any(mandate =>
mandate.sb_directdebitmandateid ==
payment.sb_directdebitmandateid &&
mandate.statuscode == 1)
select payment;
var paymentLines = from paymentLine in Crm.sb_directdebitpaymentlines
where paymentLine.sb_paymentsent == isSent
&& paymentLine.statuscode == status
&& payments.Any(payment =>
payment.sb_directdebitpaymentid ==
paymentLine.sb_directdebitpaymentid &&
payment.statuscode == 1)
select paymentLine;
如果有人对如何解决此问题有任何想法,我将非常感激!
I'm experiencing this error...
variable 'paymentLine' of type 'Xrm.sb_directdebitpaymentline' referenced from scope '', but it is not defined
... when I attempt to do 'paymentLines.ToList()' after the following code:
var payments = from payment in Crm.sb_directdebitpayments
where payment.statuscode == 1
&& Crm.sb_directdebitmandates.Any(mandate =>
mandate.sb_directdebitmandateid ==
payment.sb_directdebitmandateid &&
mandate.statuscode == 1)
select payment;
var paymentLines = from paymentLine in Crm.sb_directdebitpaymentlines
where paymentLine.sb_paymentsent == isSent
&& paymentLine.statuscode == status
&& payments.Any(payment =>
payment.sb_directdebitpaymentid ==
paymentLine.sb_directdebitpaymentid &&
payment.statuscode == 1)
select paymentLine;
If anyone has any ideas on how to fix this, I'd be very grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是查询语法和扩展方法(Any)的奇怪组合。
它看起来像是捕获循环变量问题的变体。
paymentLines 的执行被推迟,那么当执行
payments.Any( ) 时,捕获的
paymentLine 行有什么值呢?
尝试将其重写为 Join。
It is a curious combination of query syntax and extension methods (Any).
It looks like a variation on the capture loop var problem. Execution of
paymentLines
is deferred, so what value does the capturedpaymentLine
line have when thepayments.Any( )
is executed?Try to rewrite it as a Join.
我之前曾尝试将其编写为连接,但惨败。然而今天我重新审视并找出了我做错的地方;这与 where 子句的顺序有关(事实上,我花了一段时间才弄清楚我什至需要多个 where 子句!)。感谢您的所有回答。解决方案是:
I had tried writing this as a join previously, but failed miserably. However I revisited is today and worked out what I'd done wrong; it was to do with the order of the where clauses (indeed it took me a while to work out I even needed multiple where clauses!). Thanks for all answers. The solution was:
我相信您不能在
Select
之前调用Any()
,因为Any()
返回一个boolean
值。我会将Any()
中的所有代码放入Where
中,然后在您之前检查Any()
foreach
子句。I believe that you cannot call
Any()
before aSelect
asAny()
returns aboolean
value. I would take all of the code you have inside theAny()
and put it in theWhere
and then check forAny()
before youforeach
clause.