C# 如何使用集合类中的方法对列表排序

C# 如何使用集合类中的方法对列表排序,c#,C#,我的搜索方法有问题。 我想在我的collection类中创建一个函数,该函数可以在我的列表中找到具有特定名称的所有产品(对象),然后根据价格对列表进行排序 但由于它是通用的,我无法“访问”列表中对象中的字段。 谁能帮帮我吗 以下是我尝试过的: public class ProductList<T> : ICollection<T>, IComparer<T> { public List<T> _productList = new List&

我的搜索方法有问题。 我想在我的collection类中创建一个函数,该函数可以在我的列表中找到具有特定名称的所有产品(对象),然后根据价格对列表进行排序

但由于它是通用的,我无法“访问”列表中对象中的字段。 谁能帮帮我吗

以下是我尝试过的:

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..
    }
}
公共类产品列表:ICollection,IComparer
{
公共列表_productList=新列表();
//其他一些方法
//1.尝试:
public void FuncSearch\u Name(字符串search\u Name、ProductList ListIn、out ProductList ListOut)
{
ListOut=ListIn.Where(x=>x.Name.Equals(search_Name)).ToList();//这里它抱怨它不能隐式地将泛型列表转换为productlist(我真的不明白)
}
//我也试过这样做:
public void FuncSearch\u name(字符串search\u name、ref List ListIn、out List ListOut)
{
ListOut=ListIn.Where(x=>x.Name.Equals(search_Name)).ToList();
ListOut.OrderByDescending(o=>o.Price.ToList();//在这里找不到名称和价格,因为“t”不包含它们。。
}
}

谢谢你抽出时间。我希望您理解我的问题并能帮助我。

好吧,实际上您正在实现
IComparer
。这是用来对
T
对象排序的比较器吗?如果是,您可以将其用于
列表.Sort
,这与
OrderByDescending
不同:

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.Sort(this);
}

您可以在不实现自己的集合类和
IComparer

的情况下使用它,因为您的类是泛型的,并且泛型参数不受限制,
T
可以是任何内容,因此编译器无法知道您的对象具有
名称
属性

您的声明应该是:

public class ProductList<T> : ICollection<T>, IComparer<T> where T: Product
{
    public List<T> _productList = new List<T>();
公共类产品列表:ICollection,IComparer其中T:Product
{
公共列表_productList=新列表();
如果产品有子类,或者

public class ProductList : ICollection<Product>, IComparer<Product> 
{
    public List<Product> _productList = new List<Product>();
公共类产品列表:ICollection,IComparer
{
公共列表_productList=新列表();
另外,一个类型的集合实现
IComparer
,通常是在一个单独的类中完成的,这是不寻常的

仔细查看您的问题,
ToList
返回一个
列表
。无法将
列表
强制转换为自定义类。因为您没有发布构造函数,所以我可以看到创建新的
产品列表
的唯一方法是逐个添加项。通常,collection类型有一个需要在集合中(通常是一个简单的
IEnumerable
)初始化它


无论哪种方式,您的集合类型似乎做了很多工作,并且您尝试实现的方法根本不与类成员交互。

为什么不将ProductList限制为接口(或具体产品)

公共类产品列表:ICollection,IComparer

那么,您可以为
T
添加额外的约束:

interface IHasName
{
   string Name{get;}
}

因此,编译器会知道如何从T中获取名称。然而,您只能对您的类执行此操作。对于不受您控制的类,您只需编写一个实现此接口的包装器。

您能否将
T
限制为具有您所需的基类型或接口?我很困惑,为什么您的方法在其中
ProductList
,当您使用一个完全不同的列表时?当我按照您建议的第二个操作时,我会收到以下错误:“不一致的可访问性:字段类型”System.Collections.Generic.list“比字段”Eksamensprojekt.ProductList“更难访问”我刚刚发现这个产品是抽象的。我已经把它公开了。但是现在我有一些其他问题…(只是不是我的日子:()我有一个搜索方法:'public void FuncSearch_name(string search_name,ref List ListIn,out List ListOut){ListOut=ListIn.Where(x=>x.name.Equals(search_name)).ToList();ListOut.Sort(this);}'但是当我尝试从main调用该方法时,我得到了som错误。.我写:'mylist.FuncSearch_name(namename,ref mylist,out nylist);'我说这是无效的?当我尝试第一个方法时,您建议我仍然存在无法找到“name”的问题?
public class ProductList : ICollection<IProduct>, IComparer<IProduct>
interface IHasName
{
   string Name{get;}
}