你有两个班级LabCollection
和LabEntity
. LabCollection
包含一个ArrayList
现在。
一个更好的方法是LabCollection
像这样改变类。(我宁愿命名LabCollectionManager
)
public class LabCollectionManager()
{
//.................
public List<LabEntity> GetAllLabEntities()
{
//method that generates a generic list of LabEntity
}
public List<LabEntity> GetLabEntitiesByLabName(string labName)
{
return GetAllLabEntities().Where(le => le.LabName == labName).ToList();
}
//.................
}
现在像这样在代码隐藏中调用它
var labManager = new LabCollectionManager();
gridview.DataSource = labManager.GetLabEntitiesByLabName("CPT");
gridview.DataBind();
更新:
如果您想坚持使用 ArrayList,请像这样更改您的函数
public class LabCollectionManager()
{
//.................
public ArrayList GetAllLabEntities()
{
//method that generates a generic list of LabEntity
}
public ArrayList GetLabEntitiesByLabName(string labName)
{
var completeList = GetAllLabEntities();
var filteredList = new ArrayList(completeList.Cast<LabEntity>()
.Where(le => le.LabName == labName)
ToList());
return filteredList;
}
//.................
}
PS:不是很优化,但这将帮助您入门。