C# List.Clear() 方法没有得到正确的响应

C# List.Clear() method not getting correct response

我正在尝试使用以下代码将下面的 class 映射到目的地 class。这里试图将 Employee class 映射到

        static void Main(string[] args)
        {
            var emp = new List<Employee>()
            {
                new Employee()
                {
                    FirstName = "Test",
                    LastName = "Performance",
                    ID="1",
                    Availabities = new List<Availability>()
                    {
                        new Availability()
                        {
                            BeginDate = DateTime.Now,
                            EndDate = DateTime.Now.AddDays(1)
                        }
                    }
                 },
                 new Employee()
                 {
                     FirstName = "Test1",
                     LastName = "Performance1",
                     ID="2",
                     Availabities = new List<Availability>()
                    {
                        new Availability()
                        {
                            BeginDate = DateTime.Now,
                            EndDate = DateTime.Now.AddDays(10)
                        }
                     }
                 },
                  new Employee()
                 {
                     FirstName = "Test123",
                     LastName = "Performance1",
                     ID="3",
                     Availabities = new List<Availability>()
                    {
                        new Availability()
                        {
                            BeginDate = DateTime.Now,
                            EndDate = DateTime.Now.AddDays(5)
                        },
                        new Availability()
                        {
                            BeginDate = DateTime.Now,
                            EndDate = DateTime.Now.AddDays(3)
                        }
                     }
                 }
            };

这里是目标对象映射,这里是要用EmployeeDest映射的Employee。

            List<AvailabilityDest> empAvailabilitiesDest = new List<AvailabilityDest>();

            var results = new List<EmployeeDest>();

            foreach (var token in emp)
            {
                empAvailabilitiesDest.Clear();

                foreach (var item in token.Availabities)
                {
                    
                    var empAvailability = new AvailabilityDest
                    {
                        BeginDateDest = item.BeginDate,
                        EndDateDest = item.EndDate,
                    };
                        empAvailabilitiesDest.Add(empAvailability);
                }
                var employee = new EmployeeDest
                {
                    FirstNameDest = token.FirstName,
                    LastNameDest = token.LastName,
                   IDDest =token.ID,
                    AvailabitiesDest = empAvailabilitiesDest
                };
                results.Add(employee);
               
            }
            Console.WriteLine(results);
        }

这里 empAvailabilitiesDest.Clear() 没有清除列表,availabilityDest 每次迭代都在增加。

我在这里遗漏了一些东西。

  1. 如何优化此处的代码以获得更好的性能?

大概这里的真正问题是您所有的员工对象都共享 相同的 列表;不管你是否清除它、添加东西等等:如果有一个列表并且它在所有员工之间共享,那么对该列表的更改将针对 every 员工显示,并且它会显得不正确。

据推测,您真的只想将列表创建移动到内部,每个员工 foreach

var results = new List<EmployeeDest>();
foreach (var token in emp)
{
    List<AvailabilityDest> empAvailabilitiesDest = new List<AvailabilityDest>
();
    // your previous code unchanged
}

1.您的代码有问题

        var employee = new EmployeeDest
        {
            FirstNameDest = token.FirstName,
            LastNameDest = token.LastName,
            IDDest = token.ID,
            AvailabitiesDest = empAvailabilitiesDest // This is problematic code
        };

由于上面的代码,每个 empolyee.AvailabitiesDest 都将引用同一个对象,因为它不是原始类型变量。 您必须将对象的副本分配给 employee.AvailabitiesDest、

        var employee = new EmployeeDest
        {
            FirstNameDest = token.FirstName,
            LastNameDest = token.LastName,
            IDDest = token.ID,
            AvailabitiesDest = new List<AvailabilityDest>(empAvailabilitiesDest)
        };

2。简化代码

使用 Linq。

        results = emp.Select(token => new EmployeeDest
        {
            FirstNameDest = token.FirstName,
            LastNameDest = token.LastName,
            IDDest = token.ID,
            AvailabitiesDest = token.Availabities.Select(item => new AvailabilityDest
            {
                BeginDateDest = item.BeginDate,
                EndDateDest = item.EndDate,
            }).ToList()
        }).ToList();

或者使用AutoMapper

    var mapper = new Mapper(new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<Employee, EmployeeDest>()
            .ForMember(x => x.FirstNameDest, opt => opt.MapFrom(src => src.FirstName))
            .ForMember(x => x.LastNameDest, opt => opt.MapFrom(src => src.LastName))
            .ForMember(x => x.IDDest, opt => opt.MapFrom(src => src.ID))
            .ForMember(x=> x.AvailabitiesDest, opt => opt.MapFrom(src => src.Availabities));
        cfg.CreateMap<Availability, AvailabilityDest>()
            .ForMember(x => x.BeginDateDest, opt => opt.MapFrom(src => src.BeginDate))
            .ForMember(x => x.EndDateDest, opt => opt.MapFrom(src => src.EndDate));
    }));

    var results = mapper.Map<List<EmployeeDest>>(emp);