我发生了一个奇怪的情况,或者这可能是我对列表的误解,或者是你看了太久并且遗漏了一些简单的事情之一。
当我在此示例中重新排序列表时,它会在列表中的行之间混合列值。以下是一些照片作为调试过程中捕获的示例。
在下面的图片中,首先注意在783上完成的列表排序。接下来记下列表中位置[0]的ItemId值。这是1121年
现在,在从第793行推进到798之后,请注意inventoryNeed
之后的值lstInventoryNeeds[i]
。private ActionConfirmation<int> AllocateContainersForNeedList(List<AllocationNeedViewModel> lstInventoryNeedsOriginal, int intUserId, bool failOnShortage)
{
//Sort by this so if first alloc attempt fails for the remnant,
//then add that qty to next Need in list (if exists) and attempt allocate for full sheets
var lstInventoryNeeds = lstInventoryNeedsOriginal;
//.OrderBy(x => x.ItemId)
//.ThenByDescending(x => x.IsRemnant)
//.ToList();
ActionConfirmation<int> allocateResult = ActionConfirmation<int>.CreateSuccessConfirmation("Allocation of Containers for Needs List Successful.",1);
bool firstOfTwoNeedsForSameItem = false;
AllocationNeedViewModel inventoryNeed;
for (int i = 0; i < lstInventoryNeeds.Count; i++)
{
inventoryNeed = lstInventoryNeeds[i];
//If this is an inventory tracked item, allocate it
if (boolIsInvenTrackedItem(inventoryNeed.ItemId))
{
//Allocate the required inventory for the need just created
if (lstInventoryNeeds.Count() > i + 1)
{
firstOfTwoNeedsForSameItem = inventoryNeed.IsRemnant && (lstInventoryNeeds[i + 1].ItemId == inventoryNeed.ItemId);
}
else
{
firstOfTwoNeedsForSameItem = false;
}
allocateResult =
AllocInvenForNeed(
inventoryNeed.FacilityId,
inventoryNeed,
intUserId,
0,
0,
!firstOfTwoNeedsForSameItem
);
//Create Allocation Error
if (!allocateResult.WasSuccessful)
{
//Check if original attempt was for a Remnant
if (firstOfTwoNeedsForSameItem)
{
//Add current remnant need to the next need for this item, which will then get allocated on the next loop
lstInventoryNeeds[i + 1].QtyNeeded = lstInventoryNeeds[i + 1].QtyNeeded + inventoryNeed.QtyNeeded;
}
else
{
return allocateResult;
}
}
} //if inventory tracked item
}//for loop
return allocateResult;
}
。请注意,其ItemId值为1336!而且,位置[0]中的相同列表条目也将ItemId值从1121更改为1336!刚刚发生了什么?!?!
现在,如果我在第783行注释掉原始列表重新排序代码并再次运行它,请参阅此图片,当i = 0之前就在inventoryNeed赋值之后捕获,就像之前一样,但是在ItemId中没有混淆值。
知道为什么会这样吗?
以下是本节的代码,但不确定它是脱离上下文的有用之处:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var lstInventoryNeedsOriginal = new List<AllocNeedVM>();
int b = 5;
for (int a = 0; a <= b; a++)
{
int c = b - a;
var allocNeed = new AllocNeedVM()
{
ItemId = c,
ItemDesc = "Item " + c.ToString(),
IsRemnant = false
};
lstInventoryNeedsOriginal.Add(allocNeed);
}
AllocNeedVM inventoryNeed;
Console.WriteLine("List before sort.");
for (int i = 0; i < lstInventoryNeedsOriginal.Count; i++)
{
inventoryNeed = lstInventoryNeedsOriginal[i];
Console.WriteLine("{0}, {1}, {2}", inventoryNeed.ItemId, inventoryNeed.ItemDesc, inventoryNeed.IsRemnant);
}
var lstInventoryNeeds = lstInventoryNeedsOriginal
.OrderBy(x => x.ItemId)
.ThenByDescending(x => x.IsRemnant)
.ToList();
Console.WriteLine("List after sort.");
for (int i = 0; i < lstInventoryNeeds.Count; i++)
{
inventoryNeed = lstInventoryNeeds[i];
Console.WriteLine("{0}, {1}, {2}", inventoryNeed.ItemId, inventoryNeed.ItemDesc, inventoryNeed.IsRemnant);
}
Console.ReadLine();
}
}
public class AllocNeedVM
{
public int ItemId { get; set; }
public string ItemDesc { get; set; }
public bool IsRemnant { get; set; }
}
}
这是一个MCVE,但我不能复制这个问题......但也许我错过了一些会产生影响的关键因素
padding-bottom