下面是我们在我们正在构建的 ASP MVC3 站点中使用的两个 ViewModel。在下面的代码中,我试图填充名为的变量TradingPartners
IList
的AgentIdDetail
属性。AgentWithTraining
bigAgent
为了更好地说明:
bigAgent
是一个AgentWithTraining
AgentWithTraining
有一个AgentIdDetail
ICollection
列表对象作为属性
AgentIdDetail
有一个IList
名为的列表对象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; }
}
问题是以下代码行:
bigAgent.AgentIdDetails = new AgentIdDetail();
这给了我错误:
Cannot implicitly convert type 'Monet.ViewModel.AgentIdDetail' to 'System.Collections.Generic.ICollection<Monet.ViewModel.AgentIdDetail>'. An explicit conversion exists (are you missing a cast?)
有人可以解释我需要如何进行初始化bigAgent.AgentIdDetails
吗?以下是我正在使用的完整代码部分,以防万一。
//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);
}