Below are two ViewModels we use in an ASP MVC3 site we're building. In the code below that, I am trying to populate the TradingPartners
IList
property of the AgentIdDetail
property of the AgentWithTraining
variable named bigAgent
.
To better illustrate:
bigAgent
is a AgentWithTraining
AgentWithTraining
has an AgentIdDetail
ICollection
list object as a property
AgentIdDetail
has an IList
list object by the name of TradingPartner
public class AgentWithTraining
{
public Monet.Models.Agent Agent { get; set; }
public ICollection<AgentProdTrainDetail> AgentProdTrainDetails { get; set; }
public ICollection<AgentIdDetail> AgentIdDetails { get; set; }
}
public class AgentIdDetail
{
public string AgentId { get; set; }
public string CompanyCode { get; set; }
public IList<string> TradingPartners { get; set; }
}
The problem is the following line of code:
bigAgent.AgentIdDetails = new AgentIdDetail();
This gives me the error:
Cannot implicitly convert type 'Monet.ViewModel.AgentIdDetail' to 'System.Collections.Generic.ICollection<Monet.ViewModel.AgentIdDetail>'. An explicit conversion exists (are you missing a cast?)
Can someone explain how I need to go about initializing bigAgent.AgentIdDetails
? Below is the full section of code I am using, just in case that is helpful.
//Grab Trading partner information and add it to 'trainlist' object
List<AgentIdToTradingPartner>tradingParter = new List<AgentIdToTradingPartner>();
var symNumToAgId = from s in db.SymetraNumberToAgentId
where s.SymetraNumber == id
select s;
//*******************************************
//This line is causing the problem
//*******************************************
bigAgent.AgentIdDetails = new AgentIdDetail();
foreach (var s in symNumToAgId)
{
AgentIdDetail item = new AgentIdDetail();
item.AgentId = s.AgentId;
item.CompanyCode = s.CompanyCode;
tradingParter = db.AgentIdToTradingPartner
.Where(r => r.AgentId == s.AgentId).ToList();
item.TradingPartners = new List<string>();
foreach (var t in tradingParter)
{
item.TradingPartners.Add(t.TradingPartner.ToString());
}
bigAgent.AgentIdDetails.Add(item);
}