[英]Add few properties of class in list
我有一個聲明了8個屬性的類:
public class classProduct
{
public int iProductID { get; set; }
public string sPName { get; set; }
public string sPDescription { get; set; }
public int iPTypeID { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
我有一種方法,可以在清單中添加4個屬性,例如,ProductDetailsTbls是我的Tbl1,ProductTypeTbls是Tbl2。
public List<classProduct> GetAllProduct()
{
List<classProduct> lst = new List<classProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new classProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
但是它將返回我不需要的所有屬性。
如果您只想要一些屬性,我想您將不得不引入一個新類。
我的方法是用四個屬性定義一個基類,然后您可以classProduct
繼承classProduct
。
您的基類如下所示:
public class baseClassProduct
{
public string sPName { get; set; }
public string sPDescription { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
}
和您的classProduct:
public class classProduct : baseClassProduct
{
public int iProductID { get; set; }
public int iPTypeID { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
在您的GetAllProduct-Method中,您可以使用baseClass,並且您將僅獲得所需的信息。
public List<baseClassProduct> GetAllProduct()
{
List<baseClassProduct> lst = new List<baseClassProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new baseClassProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.