为什么此类型不能分配给参数类型?

时间:2016-04-15 14:06:22

标签: c# automapper

OWF_ManagersNextTwoMonths返回类型是OWF_ManagerRelationshipViewModel的一个副本。此方法是原始的修改版本,使用OWF_ManagerRelationshipViewModel作为返回类型。目标是消除其中一个代理类以减少返回的记录数。

另一个区别是在return语句中将新OWF_ManagersNextTwoMonthsViewModel传递给automapper

我在返回类型的managerListFinal.Add(item);部分的(managerListFinal)automapper项下获得了红色波浪线。

managerListFinal.Add(item);中的第一个红色波形说:

  

'参数类型Jupiter.Core.Model.OWF_ManagerRelationship不是   可分配给参数类型   Jupiter.Core.Model.OWF_ManagersNextTwoMonthsViewModel'

(managerListFinal)中的第二个红色波形说:

  

'参数类型Jupiter.Core.Model.OWF_ManagersNextTwoMonthsViewModel   不能分配给参数类型   Jupiter.Core.Model.OWF_ManagerRelationship'

我还尝试在managerListFinal返回下方返回automapper,但收到的消息与上面第一个使用automapper的内容相同。

新的OWF_ManagersNextTwoMonthsViewModel是在文档请求列表中创建的,目的是为了不返回这些记录。我认为它会起作用,因为它与原始的OWF_ManagerRelationshipViewModel非常相似。

我认为automapper返回会返回我需要的内容,如果它正常工作的话。我试图获取onsites日期,目标日期,用户名和经理类型。现场和目标日期在下面的方法中使用,而DisplayName,Manager Type和Users显示在视图的网格中。

我认为这就像复制viewmodel并省略它的doc request list部分一样简单。我不明白为什么编译器不喜欢这个或如何纠正它。

原始OWF_ManagerRelationshipViewModel:

public class OWF_ManagerRelationshipViewModel 
{
    public OWF_ManagerRelationshipViewModel()
    {
        OWF_Onsites = new List<OWF_OnsitesViewModel>();
        OWF_DocumentRequestList = new List<OWF_DocumentRequestListViewModel>();
    }

    public int RelationshipId { get; set; }

    [Required]
    public int ManagerId { get; set; }
    public string Users { get; set; }
    public string ManagerType { get; set; }

    [Required]
    public string DisplayName { get; set; }
    public string CurrentState { get; set; }
    public Nullable<bool> IsActive { get; set; }

    public IEnumerable<OWF_AccessGroupViewModel> UsernameList { get; set; }
    public virtual ICollection<OWF_DocumentRequestListViewModel> OWF_DocumentRequestList { get; set; }
    public virtual ICollection<OWF_OnsitesViewModel> OWF_Onsites { get; set; }
}

我的新OWF_ManagersNextTwoMonthsViewModel:

     public class OWF_ManagersNextTwoMonthsViewModel 
{
    public OWF_ManagersNextTwoMonthsViewModel()
    {
        //OWF_DocumentRequestList = new List<OWF_DocumentRequestListViewModel>();
        OWF_Onsites = new List<OWF_OnsitesViewModel>();
    }

    public int RelationshipId { get; set; }

    [Required]
    public int ManagerId { get; set; }
    public string Users { get; set; }             
    public string ManagerType { get; set; }         

    [Required]
    public string DisplayName { get; set; }
    public string CurrentState { get; set; }
    public Nullable<bool> IsActive { get; set; }

    public IEnumerable<OWF_AccessGroupViewModel> UsernameList { get; set; }                                 
    //public virtual ICollection<OWF_DocumentRequestListViewModel> OWF_DocumentRequestList { get; set; }    
    public virtual ICollection<OWF_OnsitesViewModel> OWF_Onsites { get; set; }
}

红色波浪形的方法:

        public IEnumerable<OWF_ManagersNextTwoMonthsViewModel> GetAllExistingManagersByCurrentDate()
    {
        var managers = _relationshipRepo.GetAll();
        var managerListFinal = new List<OWF_ManagersNextTwoMonthsViewModel>();
        var year = DateTime.Now.Year;
        var prevYear = DateTime.Now.AddYears(-1).Year;

        foreach (var item in managers)
        {
            foreach (var onsite in item.OWF_Onsites.Where(x => x.OnsiteDate != null))
            {
                if (Convert.ToDateTime(onsite.OnsiteDate).Month == DateTime.Now.Month && Convert.ToDateTime(onsite.OnsiteDate).Year == year  ||
                    onsite.TargetMonth == DateTime.Now.Month && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
                    onsite.TargetMonth == (DateTime.Now.Month + 1) && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
                    Convert.ToDateTime(onsite.OnsiteDate).Month == (DateTime.Now.Month + 1) && Convert.ToDateTime(onsite.OnsiteDate).Year == year ||
                    Convert.ToDateTime(onsite.OnsiteDate).Month == DateTime.Now.Month && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear ||
                    onsite.TargetMonth == DateTime.Now.Month && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear ||
                    onsite.TargetMonth == (DateTime.Now.Month + 1) && onsite.OnsiteDate == null && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear ||
                    Convert.ToDateTime(onsite.OnsiteDate).Month == (DateTime.Now.Month + 1) && Convert.ToDateTime(onsite.OnsiteDate).Year == prevYear)
                {
                    managerListFinal.Add(item);
                }
            }
        }
        //return Mapper.Map<IEnumerable<OWF_ManagerRelationship>, IEnumerable<OWF_ManagersNextTwoMonthsViewModel>>(managerListFinal);
        return managerListFinal;
    }

1 个答案:

答案 0 :(得分:0)

您从OWF_ManagerRelationship中获取项目,然后将它们放入OWF_ManagersNextTwoMonthsViewModel列表中。之后你想要映射它们。

我认为你应该做这样的事情

managerListFinal.Add(Mapper.Map<OWF_ManagerRelationship,OWF_ManagersNextTwoMonthsViewModel>(item));

你应该很好