一个列表的更改如何影响完全不同的列表?

发布于 2024-12-15 04:05:17 字数 1149 浏览 3 评论 0原文

我有以下代码:

jobsToShiftUp.AddRange(PresentationManager.Instance.ScheduleViewSource.Where(o => o.Resources.First().ResourceName == criticalJob.Resources.First().ResourceName && ((o.Start >= criticalJob.Start) || (o.Start < criticalJob.Start && o.End > criticalJob.Start)) && !o.JobGuid.Equals(((Job)state.DraggedAppointments.First()).JobGuid) && GetWeekNumber(o.Start) == GetWeekNumber(criticalJob.Start)));
jobsToShiftUp = jobsToShiftUp.OrderBy(o => o.Start).ToList();
TimeSpan timeToShift = criticalJob.End - jobsToShiftUp.First().Start;

foreach (Job job in jobsToShiftUp)
{
    if (jobsToShiftUp.IndexOf(job) == 0)
    {
        ((ObservableCollection<Job>)state.DestinationAppointmentsSource).Single(o => o.JobGuid.Equals(job.JobGuid)).Start += timeToShift;
        ((ObservableCollection<Job>)state.DestinationAppointmentsSource).Single(o => o.JobGuid.Equals(job.JobGuid)).End += timeToShift;

......

最后两行(增加作业的开始和结束属性)同时影响 state.destinationappointmentssource 列表和 jobsToShiftUp 列表(即,它影响当前在foreach 循环)。我根本无法想象为什么它会影响 jobsToShiftUp 列表!?

I have the following code:

jobsToShiftUp.AddRange(PresentationManager.Instance.ScheduleViewSource.Where(o => o.Resources.First().ResourceName == criticalJob.Resources.First().ResourceName && ((o.Start >= criticalJob.Start) || (o.Start < criticalJob.Start && o.End > criticalJob.Start)) && !o.JobGuid.Equals(((Job)state.DraggedAppointments.First()).JobGuid) && GetWeekNumber(o.Start) == GetWeekNumber(criticalJob.Start)));
jobsToShiftUp = jobsToShiftUp.OrderBy(o => o.Start).ToList();
TimeSpan timeToShift = criticalJob.End - jobsToShiftUp.First().Start;

foreach (Job job in jobsToShiftUp)
{
    if (jobsToShiftUp.IndexOf(job) == 0)
    {
        ((ObservableCollection<Job>)state.DestinationAppointmentsSource).Single(o => o.JobGuid.Equals(job.JobGuid)).Start += timeToShift;
        ((ObservableCollection<Job>)state.DestinationAppointmentsSource).Single(o => o.JobGuid.Equals(job.JobGuid)).End += timeToShift;

......

The last two lines (increasing the start and end properties of the jobs) affects both the state.destinationappointmentssource list AND the jobsToShiftUp list (i.e. it affects the job object currently being referenced in the foreach loop). I can't think why it would affect the jobsToShiftUp list at all!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-12-22 04:05:17

由于两个列表都包含相同的对象实例。
例如:

Job job = new Job();
List<Job> list1 = new List<Job>{job};
List<Job> list2 = new List<Job>{job};

list[0].Start+=10 会将 10 添加到 job 中,该作业也被 list2 引用

Since both list contain the same instances of objects.
Like:

Job job = new Job();
List<Job> list1 = new List<Job>{job};
List<Job> list2 = new List<Job>{job};

list[0].Start+=10 will add 10 to job which is also referenced by list2

寂寞清仓 2024-12-22 04:05:17

我认为这两个对象具有相同的引用。

因此,jobsToShiftUp 集合和 state.DestinationAppointmentsSource 可能都具有一些相同的引用,并更新反映在另一个引用中的引用。

您可以使用克隆(深复制)来避免这种情况。

I think both object have same reference.

So jobsToShiftUp collection and state.DestinationAppointmentsSource might both have some same reference and updating the one reflecting in another.

You can use cloning (deep copy) to avoid this .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文