简体   繁体   English

如何在C#中编辑匿名动态列表

[英]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. 我从linq到sql创建的动态列表具有一个额外的属性( NewStatusId ),稍后我将使用同一方法对其进行更新。 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 . 必须将现有动态列表复制到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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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