C# 列表<&燃气轮机;对象、不同类型、单独排序和拉出类型?
我有一些产品,任何、所有或没有一个可能与特定提交相关。所有7个产品都是该类产品的子类。我需要存储与提交相关联的所有产品,然后在我的表示层上检索它们及其字段数据。我一直在使用C# 列表<&燃气轮机;对象、不同类型、单独排序和拉出类型?,c#,asp.net,visual-studio,oop,C#,Asp.net,Visual Studio,Oop,我有一些产品,任何、所有或没有一个可能与特定提交相关。所有7个产品都是该类产品的子类。我需要存储与提交相关联的所有产品,然后在我的表示层上检索它们及其字段数据。我一直在使用列表和列表,但是当我使用类型的时,我抛出了一个错误,表示我无法隐式地将systems.generic.IEnumerable转换为类型“Product”。我试过投,但没有成功 当我使用prodlist.OfType()时没有错误,但是当我尝试将其存储在EPL“tempEpl”的实例中时,我得到了前面提到的与cast相关的错误。
列表
和列表
,但是当我使用类型的时,我抛出了一个错误,表示我无法隐式地将systems.generic.IEnumerable
转换为类型“Product”。我试过投,但没有成功
当我使用prodlist.OfType()时代码>没有错误,但是当我尝试将其存储在EPL“tempEpl”的实例中时,我得到了前面提到的与cast相关的错误。有什么好处?代码如下
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.
}
ProductService pserv=newproductservice();
IList prodlist=pserv.getProductById(x);
EPL tempEpl=新的EPL();
if((prodlist.OfType())!=null)
{
tempEpl=prodlist.OfType();//这会引发转换错误。
}
数据层
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);
}
List TempProdList=new List();
conn.Open();
SqlCommand EplCmd=新的SqlCommand(EPLQuery,conn);
SqlDataReader EplRead=null;
EplRead=EplCmd.ExecuteReader();
EPL TempEpl=新的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.1=EplRead.GetInt32(11);
TempProdList.Add(TempEpl);
}
此代码毫无意义:
Product tempEpl = new EPL();
if ((prodlist.OfType<EPL>()) != null)
{
prodlist.OfType<EPL>();
}
如果prodList
中没有类型为EPL
的元素,或者列表中的第一个EPL
元素,则将null
的值分配给tempEpl
(不清楚为什么要从数据层开始返回列表。为什么不返回列表?)我认为在DAL中,与其返回对象的列表
类型,不如返回产品的列表
类型。如果这样做,则无需再次将其强制转换为Product
类型
第二件事,在PL中,不要使用IList
,只需使用飞行的List
。谢谢至于如果!null,我需要确定是否有EPL产品附加到提交。我想我可以用新的tempEpl值运行if语句。
Product tempEpl = prodList.OfType<EPL>().FirstOrDefault();