简体   繁体   中英

How to edit anonymous dynamic list in c#

I am trying to create a dynamic list to act as an in memory working table in a single method. The dynamic list I create from linq to sql with an extra property ( NewStatusId ) that I'd like to update later in the same method. This method is a one off so it doesn't make sense to create a specific object class for this.

How can I achieve this result? I'm open to any means that doesn't involve created a dedicated class object, unless of course, I have to.

var lstApprovedButNotStartedWorkOrderDetailsItem = _workOrderDetailRepository.
    SearchFor(wod =>
        wod.ItemId == intItemId
    )               
    .OrderByDescending(wod => wod.WorkOrderHeader.OrderDetail.OrderHeader.RushOrderFlag)
    .Select(wod => new {
        wod.Id,
        wod.WorkOrderHeaderId,
        wod.ItemId,
        OriginalStatusId = wod.WorkOrderHeader.StatusId,
        NewStatusId = wod.WorkOrderHeader.StatusId,
        NeededQty = wod.EstimatedQuantity - wod.ActualQuantity,
        wod.Item.QtyAvailable
    })
    .ToList();

if (lstApprovedButNotStartedWorkOrderDetailsItem.Count <= 0) return ActionConfirmation<int>.CreateSuccessConfirmation("No open work orders for item", -1);

//loop through work order details in order, subtracting that line item's needs from the total available
for (int i = 0; i < lstApprovedButNotStartedWorkOrderDetailsItem.Count; i++)
{
    var wod = lstApprovedButNotStartedWorkOrderDetailsItem[i];
    //if wod 
    lstApprovedButNotStartedWorkOrderDetailsItem[i].NewStatusId = decTotalUnitsAvailable >= wod.NeededQty
        ? (int)WorkOrderStatus.Released
        : (int)WorkOrderStatus.InventoryHold;
}

You can add to an existing list of anonymous types by adding another "anonymous type" with the same properties as the ones in the list:

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        var list = (new [] { 1, 2, 3 })
            .Select(i => new { Value = i })
            .ToList();

        list.Add(new { Value = 4 });

        foreach (var i in list)
            Console.WriteLine(i);
    }
}

However, anonymous types are immutable. They are only projections of existing data and calculations. So you're better off just making a custom type for this ... it doesn't have to be very onerous, just make a new type that inherits from the "old" type that you're working with:

public class CustomWorkOrderDetailsItem : WorkOrderDetailsItem
{
    public int NewStatusId {get;set;}
}

Had to copy the existing dynamic list to a new dynamic list of ExpandoObjects . Not the cleanest, but better than creating a dedicated class (for me):

var workOrderStatusList = new List<dynamic>();

//loop through work order details in order, subtracting that line item's needs from the total available
for (int i = 0; i < lstApprovedButNotStartedWorkOrderDetailsItem.Count; i++)
{
    //get wod instance
    var wod = lstApprovedButNotStartedWorkOrderDetailsItem[i];

    //Add new list item with updated status
    workOrderStatusList.Add(new ExpandoObject());
    workOrderStatusList[i].WorkOrderHeaderId = wod.WorkOrderHeaderId;
    workOrderStatusList[i].OriginalStatusId = wod.OriginalStatusId;
    workOrderStatusList[i].NewStatusId = decTotalUnitsAvailable >= wod.NeededQty
        ? (int)WorkOrderStatus.Released
        : (int)WorkOrderStatus.InventoryHold;

    //decrement allocated qty from available
    decTotalUnitsAvailable -= wod.NeededQty;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM