将一个列表复制到另一个列表

时间:2011-01-21 13:11:58

标签: c# list

我有一个viewmodel列表,其中包含此类车辆数据 public IList VehicleViewModelList {get;组; }初始化为此

  VehicleViewModelList = new List<VehicleViewModel>();

包含与IParking_Vehicle接口相同的属性,即

    string Type { get; set; }

    string Make { get; set; }

    string Model { get; set; }

    string RegNo { get; set; }

    string DriverName { get; set; }        

    string User { get; set; }
    System.DateTime Time { get; set; }

public class VehicleViewModel
{
    [Required(ErrorMessage = "Registration is required")]
    [DisplayName("Vehicle Registration")]
    public String RegNo { get; set; }

    [Required(ErrorMessage = "Make is required")]
    [DisplayName("Vehicle Make eg Jaguar etc")]
    public String Make { get; set; }

    [Required(ErrorMessage = "Model is required")]
    [DisplayName("Vehicle Model eg XFK")]
    public String Model { get; set; }

    [DisplayName("Car Type")]
    public IEnumerable<SelectListItem> CarType { get; set; }

    [Required(ErrorMessage = "Car type is required")]
    [DisplayName("Vehicle Type")]
    public string CarTypeValue { get; set; }

    public VehicleViewModel()
    {
        CarType = new SelectList(new List<string>()
                   { "CompanyCar", "PersonalCar" }.AsEnumerable<string>());
    }
}

现在我创建一个

列表
var _parkingVehicleList = new List<IParking_Vehicle>(), 
     I want to transfer all the values from     
 VehicleViewModelList to this _parkingVehicleList.

到目前为止,这个抱怨

for (int i = 0; i <= (_eam.No_of_Vehicles - 1); i++)
{
    _parkingVehicle.DriverName = _eam.DriverNameList[i].DriverName;
    _parkingVehicle.Active = true;
    _parkingVehicle.Make = _eam.VehicleViewModelList[i].Make;
    _parkingVehicle.Model = _eam.VehicleViewModelList[i].Model;
    _parkingVehicle.RegNo = _eam.VehicleViewModelList[i].RegNo;
    _parkingVehicle.Time = DateTime.Now;
    _parkingVehicle.ParkingApplication = _parkingApplication;
    _parkingVehicle.User = UserName.IntranetUser;

    _parkingVehicleList.Add(_parkingVehicle);

    //_parkingVehicleList[i].DriverName. = _eam.DriverNameList[i].DriverName;
    //_parkingVehicleList[i].Make = _eam.VehicleViewModelList[i].Make;
    //_parkingVehicleList[i].Model = _eam.VehicleViewModelList[i].Model;
    //_parkingVehicleList[i].RegNo = _eam.VehicleViewModelList[i].RegNo;
    //_parkingVehicleList[i].Time = DateTime.Now;
    //_parkingVehicleList[i].Type = _eam.VehicleViewModelList[i].CarTypeValue;
    //_parkingVehicleList[i].Active = true;
    //_parkingVehicleList[i].ParkingApplication = _parkingApplication;
    //_parkingVehicleList[i].User = UserName.IntranetUser;

}

_parkingVehicleList因为索引器而抱怨..(无法弄清楚原因?),所以它注释掉了......

而未注释的部分会添加两次相同的值,因此第一个值会丢失。任何解决方案??

3 个答案:

答案 0 :(得分:1)

var _parkingVehicleList = new List<IParking_Vehicle>(VehicleViewModelList);复制列表。 Herehere是证据。

答案 1 :(得分:1)

首先应创建一个新对象,然后将此对象添加到列表中。

for (int i = 0; i <= (_eam.No_of_Vehicles - 1); i++) {

   _parkingVehicle = new VehicleViewModel(); // Or any other class that is your implementation
   // The actual type of _parkingVehicle is not clear from your question

   _parkingVehicle.DriverName = _eam.DriverNameList[i].DriverName;
   // Rest of your code

代码的第二部分失败的原因:当您创建新的List&lt;&gt;它没有填充,长度为0.所以你不能只从你的列表中找到某个索引。

为什么你需要两次执行任务也不是很清楚。

答案 2 :(得分:0)

使用AutoMapper来缓解您的痛苦。

Getting Started with AutoMapper.

实施例

_parkingVehicle = Mapper.Map<IParking_Vehicle, IParking_Vehicle>(_eam);

- 的修改 -

  

你说
  _parkingVehicleList因为索引器而抱怨..(不知道为什么?),所以它注释掉了......

与数组不同,您需要在集合中创建对象实例(在您的情况下为List<T>),然后才能使用索引来使用它们。

在使用之前,使用Insert(Int32 index, T item)在特定索引处插入对象。

实施例

_parkingVehicleList.Insert(i, _parkingVehicle);

_parkingVehicleList[i].DriverName. = _eam.DriverNameList[i].DriverName;
_parkingVehicleList[i].Make = _eam.VehicleViewModelList[i].Make;
_parkingVehicleList[i].Model = _eam.VehicleViewModelList[i].Model;
_parkingVehicleList[i].RegNo = _eam.VehicleViewModelList[i].RegNo;
_parkingVehicleList[i].Time = DateTime.Now;
_parkingVehicleList[i].Type = _eam.VehicleViewModelList[i].CarTypeValue;
_parkingVehicleList[i].Active = true;
_parkingVehicleList[i].ParkingApplication = _parkingApplication;
_parkingVehicleList[i].User = UserName.IntranetUser;