我有这个模型结构:
[Table("Image")]
public abstract class CBaseImage
{
public int ID { get; set; }
[Required]
public string Extension { get; set; }
[MaxLength(11)]
[Required]
public string OriginalUrl { get; set; }
[MaxLength(11)]
[Required]
public string ThumbUrl { get; set; }
public int OrderNumber { get; set; }
}
public class CNewBuildingLayoutImage : CBaseImage
{
public int NewBuildingLayoutID { get; set; }
public virtual CNewBuildingLayout NewBuildingLayout { get; set; }
[AImageFormat(527, 584, false)]
[MaxLength(11)]
[Required]
public string WH_527_584_Url { get; set; }
[AImageFormat(304, 342, false)]
[MaxLength(11)]
[Required]
public string WH_304_342_Url { get; set; }
}
和
[Table("NewBuildingLayout")]
public class CNewBuildingLayout
{
public int ID { get; set; }
//.........
public virtual ICollection<CNewBuildingLayoutImage> Images { get; set; }
}
在某些时候,我需要使用CNewBuildingLayout.Images
作为ICollection<CNewBuildingLayoutImage>
AS ICollection<CBaseImage>
的{{1}},
但是当我这样做时:
(ICollection<CBaseImage>)someLayout.Images
我明白了:
无法投射类型的对象 'System.Collections.Generic.HashSet'1 [Models.CNewBuildingLayoutImage]' 输入 'System.Collections.Generic.HashSet'1 [Models.CBaseImage]'。
我试过
(ICollection<CBaseImage>)someLayout.Images.Cast<CBaseImage>()
同样的结果。
如何将ICollection<Subclass>
投射到ICollection<BaseClass>
?