C#Cast基类型列表到派生类型列表

时间:2015-11-24 12:50:53

标签: c# casting

我将基本类型的列表转换为派生类型列表

List<TicketDetail> ticketDetails = listResult.Cast<TicketDetail>().ToList();
List<TicketDetail> ticketDetails = listResult.ConvertAll(x => (TicketDetail) x).ToList();

我尝试了他们两个,每个人都抛出:

  

System.InvalidCastException:无法转换类型的对象   &#39; WorkOrder.Service.Ticket&#39;输入&#39; WorkOrder.Service.TicketDetail&#39;。   这是我的课程

namespace WorkOrder.Service
{
    using System;
    [DataContract]
    [KnownType(typeof(TicketDetail))]
    public partial class Ticket
    {
        public int RecordId { get; set; }
        public long TicketId { get; set; }
        public string Summary { get; set; }
        public string Details { get; set; }
        public int CategoryId { get; set; }
        public System.DateTime CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public string Status { get; set; }
        public string TicketSource { get; set; }
        public string TicketType { get; set; }
        public string ManagedByGroup { get; set; }
        public string ManagedByUser { get; set; }
        public Nullable<long> SubscriptionId { get; set; }
        public string PriorityLevel { get; set; }
        public string Impact { get; set; }
        public string ImpactOnBusiness { get; set; }
        public string Risk { get; set; }
        public string Comment { get; set; }
        public Nullable<System.DateTime> PlannedStartDatetime { get; set; }
        public Nullable<System.DateTime> ActualStartDatetime { get; set; }
        public Nullable<System.DateTime> PlannedDeadlineDatetime { get; set; }
        public Nullable<System.DateTime> ActualDeadlineDatetime { get; set; }
        public System.DateTime TransactionDatetime { get; set; }
        public string AssignedToGroup { get; set; }
        public string AssignedToUser { get; set; }
        public string CategoryName { get; set; }
        public int CreatedById { get; set; }
        public int StatusId { get; set; }
        public int SourceId { get; set; }
        public int TypeId { get; set; }
        public int ManagedByGroupId { get; set; }
        public Nullable<int> ManagedByUserId { get; set; }
        public int AssignedToGroupId { get; set; }
        public Nullable<int> AssignedToUserId { get; set; }
        public int PriorityLevelId { get; set; }
        public int ImpactLevelId { get; set; }
        public int ImpactOnBusinessLevelId { get; set; }
        public int RiskLevelId { get; set; }
    }
}

派生类:

namespace WorkOrder.Service
{
    [DataContract]
    public class TicketDetail : WorkOrder.Service.Ticket
    {
        [DataMember]
        public List<WorkLogView> WorkLogs { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

您无法从Ticket投射到TicketDetail,因为TicketDetailTicket的派生类型,但是无法绕道而行。

您需要使用Linq Select表达式创建新对象,例如: listResult.Select(item => new TicketDetail() { /* ... */ }).ToList()

修改

要避免在Select表达式中设置所有属性,请在TicketDetail中创建一个构造函数,该构造函数接受Ticket

namespace WorkOrder.Service
{
    [DataContract]
    public class TicketDetail : WorkOrder.Service.Ticket
    {
        public TicketDetail(Ticket ticket)
        {
             this.StatusId = ticket.StatusId;
        }

        [DataMember]
        public List<WorkLogView> WorkLogs { get; set; }
    }
}

然后就这样做:

List<TicketDetail> ticketDetails = listResult.Select(item => new TicketDetail(item)).ToList()