C# foreach 语句未正确将模型添加到模型列表

标签 c#

我有两个 foreach 循环,它们在我的应用程序中构建事件的时间轴。我有一个时间线列表对象,它是时间线详细信息对象的列表。代码如下所示:

var viewModel = new TimeLineListViewModel();
var timelineDetailsViewModel = new TimelineDetailsViewModel();

var referralDiaryEvents = _referralService.GetReferralDiaryEventsByReferralId((int)referralId).ToList();

var referral = _referralService.GetReferral((int)referralId);

foreach (var referralDiaryEvent in referralDiaryEvents)
{
1    timelineDetailsViewModel.EventDate = referralDiaryEvent.App_DiaryEvent.EventDateTimeScheduled;
2    timelineDetailsViewModel.EventType = referralDiaryEvent.App_DiaryEvent.Ref_EventType.Description;
3    timelineDetailsViewModel.EventDescription = referralDiaryEvent.App_DiaryEvent.EventName;

4    viewModel.TimeLineList.Add(timelineDetailsViewModel);
}

foreach (var referralStatusHistory in referral.App_ReferralStatusHistory)
{
    timelineDetailsViewModel.EventDate = referralStatusHistory.DateChanged;
    timelineDetailsViewModel.EventType = "Referral Status Changed: " + referralStatusHistory.Ref_ReferralStatus.Description;
    timelineDetailsViewModel.EventDescription = referralStatusHistory.Ref_ReferralStatusReason.Description;

    viewModel.TimeLineList.Add(timelineDetailsViewModel);
 }

这一切都应该很简单,并且应该在每个循环中添加每个项目以构建一个对象列表。问题是它没有这样做。我实际上不知道它在做什么 - 当我调试并运行循环时,它在做一些非常奇怪的事情。我已经对循环中的代码行进行了编号以供引用。它会经过第 1 行,然后经过第 2 行,然后返回到第 1 行,然后转到第 3 行,然后返回到第 2 行,然后转到第 4 行,依此类推,它在循环中来回跳转 for no明显的原因而不是直接通过循环,一行一行地运行。

然后最终它会将 timelineDetailsViewModel 添加到 TimeLineList。然后它会为下一个项目再次开始循环,但是当它再次将 timelineDetailsViewModel 添加到 TimeLineList 时,TimeLineList 计数变为 0,然后跳回到第 2 行,然后再次回到第 4 行,然后将项目添加到列表,但会覆盖之前添加到列表中的每个项目,使列表中的所有 timelineDetailsViewModel 项目都与最新添加的项目相同。因此,当它到达第二个 foreach 循环的底部时,在我的代码中列表中应该有七个独特的 timelineDetailsViewModel 项目,所有项目都有不同的细节 - 最终确实有七个项目,但它们都与在第二个 foreach 循环的最后一个循环中添加的最后一个 timelineDetailsViewModel。

有人知道这里发生了什么吗?我的 Visual Studio 坏了吗?我试过在我的机器上重新启动但无济于事。我正在使用 visual studio 2012。

最佳答案

它没有解释奇怪的调试器行为,但实际上您的代码中还有另一个错误:

您只创建了一次 TimeLineListViewModelTimelineDetailsViewModel 的实例。然后,遍历 foreach 循环,您总是会更改完全相同实例的属性。

在每次循环运行中,您都在更改您曾经创建的唯一实例的属性,并尝试将这个实例添加到您的列表中 x 次。

您需要在 foreach 循环中 创建实例:

foreach (var referralDiaryEvent in referralDiaryEvents)
{
    // create a NEW instance for every event!
    var timelineDetailsViewModel = new TimelineDetailsViewModel();

    timelineDetailsViewModel.EventDate = referralDiaryEvent.App_DiaryEvent.EventDateTimeScheduled;
    timelineDetailsViewModel.EventType = referralDiaryEvent.App_DiaryEvent.Ref_EventType.Description;
    timelineDetailsViewModel.EventDescription = referralDiaryEvent.App_DiaryEvent.EventName;

    viewModel.TimeLineList.Add(timelineDetailsViewModel);
}

关于C# foreach 语句未正确将模型添加到模型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38878701/

相关文章:

c# - 单击 ComboBoxItem 问题事件后,WPF 将 ComboBox 设置为 -1 索引

c# - 收集在设计中创建的文本框

c# - 如何获取以下 JSON 的 C# 对象

c# - 我应该在哪里存储许可证文件?

c# - 以最快的方式从有条件的列表中删除

c# - 获取调用线程

c# - 正则表达式匹配日期

c# - 新的System.Drawing.Font代码隐藏

C# MSSQL alter table 然后修改值

c# - 请求 UAC 提升 c#