C# 搜索数组列表

C# 搜索数组列表,c#,.net-3.5,arraylist,C#,.net 3.5,Arraylist,我在asp.net/C#/VS2008的web应用程序项目中有一个arraylist,我正在使用.net 3.5 我正在使用一个类向arraylist添加内容,该类的定义如下: using System.Web; class ShoppingCartDataStore { private string componentName; private string componentPrice; private string componentFileSize; p

我在asp.net/C#/VS2008的web应用程序项目中有一个arraylist,我正在使用.net 3.5

我正在使用一个类向arraylist添加内容,该类的定义如下:

using System.Web;

class ShoppingCartDataStore
{
    private string componentName;
    private string componentPrice;
    private string componentFileSize;
    private string componentDescription;

    public ShoppingCartDataStore(string componentName, string componentPrice, string componentFileSize, string componentDescription){
        this.componentName = componentName;
        this.componentPrice = componentPrice;
        this.componentFileSize = componentFileSize;
        this.componentDescription = componentDescription;
    }

    public string ComponentName
    {
        get
        {
            return this.componentName;
        }
    }

    public string ComponentPrice
    {
        get
        {
            return this.componentPrice;
        }
    }

    public string ComponentFileSize
    {
        get
        {
            return this.componentFileSize;
        }
    }

    public string ComponentDescription
    {
        get
        {
            return this.componentDescription;
        }
    }
}
我通过以下代码向arraylist添加内容:

ArrayList selectedRowItems = new ArrayList();
selectedRowItems.Add(new ShoppingCartDataStore(componentName, componentPrice, fileSize, componentDescription));
假设我想在以componentName作为键以这种方式添加几个值之后搜索这个arraylist。我尝试了以下代码,但找不到执行此操作的方法:

ArrayList temporarySelectedItemsList = new ArrayList();
ArrayList presentValue = new ArrayList();
string key = componentName; //some specific component name
temporarySelectedItemsList = selectedRowItems;
for (int i = 0; i < temporarySelectedItemsList.Count; i++)
{
    presentValue = (ArrayList)temporarySelectedItemsList[i];
}
ArrayList temporarySelectedItemsList=new ArrayList();
ArrayList presentValue=新的ArrayList();
字符串键=组件名称//某些特定的组件名称
临时SelectedItemsList=selectedRowItems;
for(int i=0;i
我病了,这是未经测试的,但我认为它会起作用。:)

List aList=newlist();
//在此处添加您的数据
字符串键=组件名称//某些特定的组件名称
//现在搜索
foreach(购物车数据存储i,以列表形式)
{
if(i.ComponentName==键)
{
//找到了,做点什么
}
}

您使用的是什么版本的.NET?如果您使用的是.NET 2.0或更高版本,那么就不应该使用
ArrayList
。除非您的目标是.NET pre v2,否则您几乎肯定应该使用泛型list@John如果我使用的是.NET2.0或更高版本,你能解释一下为什么我不应该使用arraylist吗?怎么了?如果不是arraylist,那么我应该使用什么来代替它?您想要使用的是List。它有几个优点,包括强制只添加正确的类型,在使用数据时不必强制转换数据(因为它已经知道数据中的类型),以及能够使用一些有用的运算符,如List.Sort和List.Contains。@user:我还强烈建议您不要在标题中重复标记。把标签放在标签里。我的项目中没有使用linq,你能用普通的c代码解释一下吗?谢谢
var results = selectedRowItems.OfType<ShoppingCartDataStore>().Where(x=>x.ComponentName == "foo")
ArrayList results = new ArrayList();


foreach (ShoppingCartDataStore store in selectedRowItems)
{
    if(store.ComponentName == "foo"){
        results.Add(store);
    }
}
List<ShoppingCartDataStore> aList = new List<ShoppingCartDataStore>();
// add your data here

string key = componentName; //some specific component name
// Now search
foreach (ShoppingCartDataStore i in aList)
{
    if (i.ComponentName == key)
    {
        // Found, do something
    }
}