在List <derivedclass>中转换list <baseclass>

时间:2015-10-21 17:24:28

标签: c# .net entity-framework

来自实体框架的类产品

public partial class Product
{
    public int ProductID { get; set; }
    public string ProductBarcode { get; set; }
    public int CompanyID { get; set; }
}

创建了派生类

public class ProductPlus : Product
{
    public bool Restock { get; set; }
    public DateTime? DateSold { get; set; }        
}

我从列表中的数据库中获取数据

List<Product> lstProduct = (from p in entity.Product select p).ToList();

我试过了:

List<ProductPlus> lstProductPlus = lstProduct.Cast<ProductPlus>().ToList();

List<ProductPlus> lstProductPlus = lstProduct.ConvertAll(x => (ProductPlus)x).ToList();

给出:无法投射类型&#39;产品&#39;输入&#39; ProductPlus&#39;。

var lstProductPlus = lstProduct.Select(item => item as ProductPlus).ToList();
var lstProductPlus = lstProduct.Select(item => new ProductPlus()).ToList();

给出:与lstProduct中的记录数完全相同,但都是空的

有人有解决方案吗?

4 个答案:

答案 0 :(得分:2)

将构造函数添加到以ProductPlus作为参数的Product

public class ProductPlus
{
    public ProductPlus() { }

    public ProductPlus(Product p){
        SomeProduct = p;
    }

    public Product SomeProduct { get; set; }
    public bool Restock { get; set; }
    public DateTime? DateSold { get; set; }        
}

然后在您的代码调用中:

var lstProduct = lstProduct.Select(item => new ProductPlus(item));

答案 1 :(得分:2)

为什么要继承product课程?我想你想在product课程中增加2个字段,对吗?

因为自动生成的类是partial类,而不是像你一样继承product类,所以只需再向分部类添加一个文件,如下所示:

public partial class Product
{
    public bool Restock { get; set; }
    public DateTime? DateSold { get; set; }        
}

答案 2 :(得分:2)

尝试,

 List<ProductPlus> lstProductPlus = lstProduct.ConvertAll(x => new ProductPlus()
 {
   CompanyID = x.CompanyID,
   ProductID = x.ProductID,
   ProductBarcode = x.ProductBarcode
 }).ToList()

答案 3 :(得分:0)

每个ProductPlus都是产品
您可以将ProductPlus转换为产品

但并非每件产品都是ProductPlus
您不能将产品投射到ProductPlus,除非它已经是ProductPlus

考虑一个班级

public class ProductDesign : Product
{
    public DateTime? ProjectedCompletion date { get; set; }        
}

经过测试,试试吧

{
    // ...
    public void TestInheritance()
    {
        try
        {
            Product p01 = new Product() { ProductBarcode = "p01" };
            ProductPlus pp01 = new ProductPlus() { ProductBarcode = "pp01" };
            Product p02 = (ProductPlus)pp01;
            //ProductPlus pp02 = (ProductPlus)p01; // throws a cast error

            List<Product> productList = new List<Product>();
            productList.Add(p01);
            productList.Add(pp01);

            //List<ProductPlus> productPlusList = productList.ConvertAll(x => (ProductPlus)x).ToList(); // throws a cast error 

            List<ProductPlus> productPlusList = productList.Where(x => x is ProductPlus).ToList().ConvertAll(x => (ProductPlus)x).ToList();
            foreach (ProductPlus p in productPlusList)   
                Debug.WriteLine(p.ProductBarcode);  // 1 item 

            List<ProductPlus> productPlusMajicList = productList.Select(x => new ProductPlus(x)).ToList();
            foreach (ProductPlus p in productPlusMajicList)
                Debug.WriteLine(p.ProductBarcode);   // majic 2 tiems
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }


    }
}
public partial class Product
{
    public int? ProductID { get; set; }
    public string  ProductBarcode { get; set; }
    public int? CompanyID { get; set; }
    public Product() { }
    public Product(Product p)
    {
        ProductID = p.ProductID;
        ProductBarcode = p.ProductBarcode;
        CompanyID = p.CompanyID;
    }
}

public class ProductPlus : Product
{
    public bool? Restock { get; set; }
    public DateTime? DateSold { get; set; }
    public ProductPlus() { }
    public ProductPlus(Product p) : base(p) { }
}