0

下面是我们在我们正在构建的 ASP MVC3 站点中使用的两个 ViewModel。在下面的代码中,我试图填充名为的变量TradingPartners IListAgentIdDetail属性。AgentWithTrainingbigAgent

为了更好地说明:

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);
        }
4

1 回答 1

2

您不能以这种方式初始化,因为它AgentIdDetails是一个ICollection<AgentIdDetail>而不是AgentIdDetail.

您必须初始化集合,然后在此集合上添加一个新项目。样品:

// initilize as a List<AgentIdDetail>
bigAgent.AgentIdDetails = new List<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);
}

作为一个好的实践,我喜欢在构造函数的 ViewModels 上初始化我的 Collection 属性,所以我不需要担心填充它时的初始化,除非我需要一个新的,例如:

public class AgentWithTraining
{
    public Monet.Models.Agent Agent { get; set; }
    public ICollection<AgentProdTrainDetail> AgentProdTrainDetails { get; set; }
    public ICollection<AgentIdDetail> AgentIdDetails { get; set; }

    public AgentWithTraining()
    {
        this.AgentProdTrainDetails = new List<AgentProdTrainDetail>();
        this.AgentIdDetails = new List<AgentIdDetail>();
    }
}
于 2013-02-08T23:42:42.673 回答