我有以下代码。如何将其转换为 LINQ?任何人请帮助我。
在下面的代码中,"powd"包含数据库值,purchaseOrderUpdateRequest包含用户提交的数据。
bool hasUpdate;
foreach (var item in purchaseOrderUpdateRequest.Lines)
{
var line = powd.PurchaseOrderLines.Single(p => p.ItemNumber == item.ItemNumber);
decimal quantityToReceive = item.QuantityReceived - line.QuantityReceivedToDate;
if (quantityToReceive > 0)
{
hasUpdate =true;
break;
}
}
谢谢。
所以你要做的就是是否有任何项目有更新?那只是:
var hasUpdate = purchaseOrder.UpdateRequest.Lines.Any(item =>
item.QuantityReceived < powd.PuchaseOrderLines
.Single(p => p.ItemNumber == item.ItemNumber)
.QuantityReceivedToDate)
我已经将比较更改为仅使用直接<
而不是减法和与 0 的比较,但这应该没问题。
未经测试,但这应该做同样的事情,并且可能更容易阅读:
var quantities = (from item in purchaseOrderUpdateRequest.Lines
from item2 in powd.PurchaseOrderLines
where item.ItemNumber == item2.ItemNumber
select item.QuantityReceived - item2.QuantityReceivedToDate);
hasUpdate = quantities.Any(quantityToReceive => (decimal) quantityToReceive > 0);