Func c# 中不需要的输入更改

Unwanted Change of Input in Func c#

我不知道如何调试这个问题,所以这可能是一个常见的初学者错误? 使用我的 Where & Select 的 IEnumerable im 在 DocumentPositionMetaDataItem-属性 中有一个 属性 'Amount'。 虽然 运行 您可以在下面看到的代码总是将其值(金额)更改为 'AmountToConvert' 的任何值。

public class OrderPositionItem : IDocumentPositionItem
{
    public int DocumentId { get; set; }
    public string PositionId { get; set; }
    public ArticleItem Article { get; set; }
    public CategoryItem Category { get; set; }
    public UnitItem Unit { get; set; }
    public ValueAddedTaxPositionItem ValueAddedTaxPosition { get; set; }
    public FinancialAccountingAccountItem FinancialAccountingAccount { get; set; }
    public CostCenterItem CostCenter { get; set; }
    public DocumentPositionMetaDataItem DocumentPositionMetaDataItem { get; set; }
    public decimal AmountBilled { get; set; }
    public bool IsAlreadyBilled { get; set; }
    public decimal AmountToConvert { get; set; }

正在更改的值在 MetaDataItem 中 属性。

      public int DocumentsDataNumber { get; set; }
    public string PositionID { get; set; }
    public decimal Amount { get; set; }
    public decimal PriceNoVat { get; set; }
    public decimal AddedVat { get; set; }
    public decimal Price { get; set; }
    public decimal Total { get; set; }


    public decimal TotalWithVat { get; set; }

"Amount" 正在更改为 "OrderPositionItem" 中的 "Amount To Convert" 值。我也不知道为什么。

这是我的 List (Queryable) orderPositions,orderPositions 上的所有属性 'Amount' 更改为任何 'AmountToConvert'。

 List<OrderPositionItem> orderPositions

这是我的代码,它将 OrderPositions where Convertible 转换为 DeliveryNotePositions。

     // assign selected order position items to the delivery note
            // (orderPosition has prop AmountToConvert, only convert this much and only if > 0)
            deliveryNote.DocumentPositionItems = 
                orderPositions.Any() 
                    ? orderPositions
                    .Where(OrderPositionItem.IsConvertibleToDeliveryNotePositionItemFunc)
                    .Select(OrderPositionItem.ToDeliveryNotePositionItemFunc)
                    .ToList()
                    : null;

其中函数:IsConvertibleToDeliveryNotePositionItemFunc

   public static readonly Func<OrderPositionItem, bool> IsConvertibleToDeliveryNotePositionItemFunc =
        orderPosition => !orderPosition.IsAlreadyBilled &&
                         orderPosition.AmountToConvert > 0 &&
                         orderPosition.AmountToConvert <=
                         orderPosition.DocumentPositionMetaDataItem.Amount - orderPosition.AmountBilled;/* left over amount */

Select 函数:

    public static readonly Func<OrderPositionItem, DeliveryNotePositionItem> ToDeliveryNotePositionItemFunc =
        orderPosition => (DeliveryNotePositionItem) orderPosition;

Select 函数中的 Cast:

        public static explicit operator DeliveryNotePositionItem(OrderPositionItem orderPosition)
    {
        if (orderPosition == null) throw new ArgumentNullException(nameof(orderPosition));

        var deliveryNotePositionItem = new DeliveryNotePositionItem()
        {
            // meta data
            DocumentPositionMetaDataItem = orderPosition.DocumentPositionMetaDataItem,
            // article
            Article = orderPosition.Article,
            // category
            Category = orderPosition.Category,
            // unit
            Unit = orderPosition.Unit,
            // value added tax position
            ValueAddedTaxPosition = orderPosition.ValueAddedTaxPosition,
            // cost center
            CostCenter = orderPosition.CostCenter,
            // financial accounting account
            FinancialAccountingAccount = orderPosition.FinancialAccountingAccount,
        };

        // adjust amounts and totals to given order position
        deliveryNotePositionItem.DocumentPositionMetaDataItem.Total =
            deliveryNotePositionItem.DocumentPositionMetaDataItem.Total /
            deliveryNotePositionItem.DocumentPositionMetaDataItem.Amount * orderPosition.AmountToConvert;
        deliveryNotePositionItem.DocumentPositionMetaDataItem.TotalWithVat =
            deliveryNotePositionItem.DocumentPositionMetaDataItem.TotalWithVat /
            deliveryNotePositionItem.DocumentPositionMetaDataItem.Amount * orderPosition.AmountToConvert;
        deliveryNotePositionItem.DocumentPositionMetaDataItem.Amount = orderPosition.AmountToConvert;
        return deliveryNotePositionItem;

    }

运行 提供的代码之前的值:

Property Amount & AmountToConvert before Handling

Amount after Handling, now is AmountToConvert before handling?

创建新的 DeliveryNotePositionItem:

时似乎造成混淆的是这一行
DocumentPositionMetaDataItem = orderPosition.DocumentPositionMetaDataItem

这会将 属性 设置为与 orderPosition 上相同的 DocumentPositionMetaDataItem 对象。它不会为您或其他任何人创建副本。这意味着两个对象现在共享相同的 DocumentPositionMetaDataItem.

这显然意味着当您更改它时,更改会反映在您正在查看的两个地方。您需要做的是以某种方式基于原始对象创建一个新的 DocumentPositionMetaDataItem 对象。

请问,这一行呢

deliveryNotePositionItem.DocumentPositionMetaDataItem.Amount = orderPosition.AmountToConvert;

a 结束

DeliveryNotePositionItem(OrderPositionItem orderPosition)

?