LINQ 错误:变量 'x'类型为'y'从范围“引用”,但未定义

发布于 2024-10-19 14:59:46 字数 1235 浏览 3 评论 0原文

我遇到此错误...

从范围“”引用了类型为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫嚣ゝ 2024-10-26 14:59:46

它是查询语法和扩展方法(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 captured paymentLine line have when the payments.Any( ) is executed?

Try to rewrite it as a Join.

苏辞 2024-10-26 14:59:46

我之前曾尝试将其编写为连接,但惨败。然而今天我重新审视并找出了我做错的地方;这与 where 子句的顺序有关(事实上,我花了一段时间才弄清楚我什至需要多个 where 子句!)。感谢您的所有回答。解决方案是:

var paymentLines = from paymentLine in Crm.sb_directdebitpaymentlines
                   join payment in Crm.sb_directdebitpayments on paymentLine.sb_directdebitpaymentid equals payment.sb_directdebitpaymentid
                   join mandate in Crm.sb_directdebitmandates on payment.sb_directdebitmandateid equals mandate.sb_directdebitmandateid
                   where mandate.statuscode == 1
                   where payment.statuscode == 1
                   where paymentLine.sb_paymentsent == isSent && paymentLine.statuscode == status
                   select paymentLine;

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:

var paymentLines = from paymentLine in Crm.sb_directdebitpaymentlines
                   join payment in Crm.sb_directdebitpayments on paymentLine.sb_directdebitpaymentid equals payment.sb_directdebitpaymentid
                   join mandate in Crm.sb_directdebitmandates on payment.sb_directdebitmandateid equals mandate.sb_directdebitmandateid
                   where mandate.statuscode == 1
                   where payment.statuscode == 1
                   where paymentLine.sb_paymentsent == isSent && paymentLine.statuscode == status
                   select paymentLine;
拥抱我好吗 2024-10-26 14:59:46

我相信您不能在 Select 之前调用 Any(),因为 Any() 返回一个 boolean 值。我会将 Any() 中的所有代码放入 Where 中,然后在您之前检查 Any() foreach 子句。

if (paymentLines.Any())
  {
foreach (var n in paymentLines)
         {
           //Do work
         }
  }

I believe that you cannot call Any() before a Select as Any() returns a boolean value. I would take all of the code you have inside the Any() and put it in the Where and then check for Any() before you foreach clause.

if (paymentLines.Any())
  {
foreach (var n in paymentLines)
         {
           //Do work
         }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文