【发布时间】:2011-09-23 06:24:33
【问题描述】:
我有一些产品,任何、全部或没有可能与特定提交相关联。所有 7 种产品都是 Product 类的子类。我需要存储与提交相关的所有产品,然后在我的表示层上检索它们及其字段数据。我一直在使用List<Product> 和List<object>,但是当我使用OfType<EPL(specific subclass)> 时,我抛出一个错误,说我不能将systems.generic.IEnumerable<EPL> 隐式转换为类型“产品”。我试过投,但无济于事。
当我使用prodlist.OfType<EPL>(); 时没有错误,但是当我尝试将其存储在 EPL“tempEpl”的实例中时,我得到了上述与演员表相关的错误。是什么赋予了?代码如下。
ProductService pserv = new ProductService();
IList<object> prodlist = pserv.getProductById(x);
EPL tempEpl = new EPL();
if ((prodlist.OfType<EPL>()) != null)
{
tempEpl = prodlist.OfType<EPL>(); // this throws a conversion error.
}
数据层
List<object> TempProdList = new List<object>();
conn.Open();
SqlCommand EplCmd = new SqlCommand(EPLQuery, conn);
SqlDataReader EplRead = null;
EplRead = EplCmd.ExecuteReader();
EPL TempEpl = new EPL();
if (EplRead.Read())
{
TempEpl.Entity1 = EplRead.GetString(0);
TempEpl.Employees1 = EplRead.GetInt32(1);
TempEpl.CA1 = EplRead.GetInt32(2);
TempEpl.MI1 = EplRead.GetInt32(3);
TempEpl.NY1 = EplRead.GetInt32(4);
TempEpl.NJ1 = EplRead.GetInt32(5);
TempEpl.PrimEx1 = EplRead.GetInt32(6);
TempEpl.EplLim1 = EplRead.GetInt32(7);
TempEpl.EplSir1 = EplRead.GetInt32(8);
TempEpl.Premium1 = EplRead.GetInt32(9);
TempEpl.Wage1 = EplRead.GetInt32(10);
TempEpl.Sublim1 = EplRead.GetInt32(11);
TempProdList.Add(TempEpl);
}
【问题讨论】:
标签: c# asp.net visual-studio oop