我的搜索方法有问题。我想在我的集合类中创建一个函数,它可以在我的列表中找到具有特定名称的所有产品(对象),然后按价格对列表进行排序。
但是因为它是通用的,所以我无法“访问”列表中对象中的字段。有人能帮帮我吗?
这是我尝试过的:
public class ProductList<T> : ICollection<T>, IComparer<T>
{
public List<T> _productList = new List<T>();
// Some other methods
// 1. try:
public void FuncSearch_Name(string search_name, ProductList<Product> ListIn, out ProductList<Product> ListOut)
{
ListOut = ListIn.Where(x => x.Name.Equals(search_name)).ToList(); // Here it is complaining about that it cant not implicity convert a generic list to productlist (I dont really understand that)
}
// I have also tried like this:
public void FuncSearch_name(string search_name, ref List<T> ListIn, out List<T> ListOut)
{
ListOut = ListIn.Where(x => x.Name.Equals(search_name)).ToList();
ListOut.OrderByDescending(o => o.Price).ToList(); // Here it can't find Name and Price because "T" not contains them..
}
}
感谢您的时间。我希望你能理解我的问题并能帮助我。