Copying one list to another
I have a list of viewmodels which contains vehicle data such as this public IList VehicleViewModelList { get; set; } initialized to this
VehicleViewModelList = new List<VehicleViewModel>();
which contains same pr开发者_如何学Coperty as is in the IParking_Vehicle interface i.e.
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>());
}
}
Now when I create a list of
var _parkingVehicleList = new List<IParking_Vehicle>(),
I want to transfer all the values from
VehicleViewModelList to this _parkingVehicleList.
So far this complains
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 complains because of indexer..(cant figure out why?), so its commented out...
and not commented part adds the same value twice, so first values are lost..any solutions??
You should first create a new object, and then add this object to the list.
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
The reason the second part of your code fails: When you create a new List<> it is not filled and has length 0. So you cannot just go to a certain index from your list.
It is also not very clear why you need to execute the assignment twice.
var _parkingVehicleList = new List<IParking_Vehicle>(VehicleViewModelList);
copies the list.
Here and here are the proof.
Use AutoMapper
to ease your pain.
Getting Started with AutoMapper.
Example:
_parkingVehicle = Mapper.Map<IParking_Vehicle, IParking_Vehicle>(_eam);
--EDIT--
You said:
_parkingVehicleList complains because of indexer..(cant figure out why?), so its commented out...
Unlike arrays, you need to create instances of objects in collections (in your case, List<T>
) before you can use them using index.
Use Insert(Int32 index, T item)
to insert an object at a specific index before using it.
Example:
_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;
精彩评论