功能C#中不必要的输入更改

时间:2019-02-11 10:22:26

标签: c# linq class casting func

我不知道如何调试问题,所以也许这是常见的初学者错误?  使用我的Where&Select的IEnumerable im在DocumentPositionMetaDataItem-Property中具有属性“ 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; }

“金额”更改为“ OrderPositionItem”中的“要转换的金额”值。而且我不知道为什么。

这是我的列表(可查询)orderPositions,orderPositions上的所有“金额”属性都更改为“ AmountToConvert”。

 List<OrderPositionItem> orderPositions

这是我的代码,它将OrderPositions转换为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 */

选择功能:

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

选择功能中的演员表:

        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?

2 个答案:

答案 0 :(得分:1)

似乎引起混乱的是创建新的DeliveryNotePositionItem时的这一行:

DocumentPositionMetaDataItem = orderPosition.DocumentPositionMetaDataItem

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

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

答案 1 :(得分:0)

不好意思,这行

deliveryNotePositionItem.DocumentPositionMetaDataItem.Amount = orderPosition.AmountToConvert;

结束

DeliveryNotePositionItem(OrderPositionItem orderPosition)