如何找到特定价值的arraylist

时间:2020-12-28 19:21:49

I have one array list:

我有一个数组列表:

public ArrayList GetExpenseTypes()
{
    ArrayList expArry = new ArrayList();

    using (SqlConnection conn = new SqlConnection(connStr))
    {
        string sql = "SELECT ID, TITLE";
        sql += " FROM EXPENSE_TYPE";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                expArry.Add(new ListItem(reader["TITLE"].ToString(), reader["ID"].ToString()));
            }
            reader.Close();
        }
        catch (Exception ex)
        {

        }
        finally
        {
            conn.Close();
        }
    }
    return expArry;
}

My ArrayList like this

我的ArrayList是这样的

1 name1
2 name2
3 name3
4 name4
5 name6

1 name1 2 name2 3 name3 4 name4 5 name6

if my value is 4 i need to display name4

如果我的值是4,我需要显示name4

How i achieve that?

我是如何实现的?

4 个解决方案

#1


0  

I'd recommend first changing ArrayList to List<ListItem>. If you can't do that for some reason you could use (assuming each item is unique):

我建议先将ArrayList更改为List 。如果由于某种原因你不能这样做,你可以使用(假设每个项目都是唯一的):

var x = expArry.Cast<ListItem>().SingleOrDefault(exp => exp.Value == value);
if (x != null)
    //item was found

If you know the item will always be there just use Single if it will only be there once.

如果您知道该项目将始终存在,只需使用单一,如果它只会在那里一次。

#2


1  

Instead of ArrayList you might want to use Dictionary<string,string> like this.

您可能希望像这样使用Dictionary 而不是ArrayList。 ,string>

public IDictionary<string,string> GetExpenseTypes()
{
    Dictionary<string,string> expArry = new Dictionary<string,string>();

    // Your sql code

    while (reader.Read())
    {
        expArry.Add(reader["TITLE"].ToString(), reader["ID"].ToString());
    }

    // The rest of your code
}

Then you can get the value like this

然后你可以得到这样的价值

var values = GetExpenseTypes();
string valueYouWant = values["4"];

If on the other hand your problem is that when you use the ListItems in a web control you are seeing the wrong values, then you need to swap the parameters when you create the ListItem because the first parameter is the text that is displayed and the second is the value. Like this.

另一方面,如果您在Web控件中使用ListItems时看到错误的值,那么在创建ListItem时需要交换参数,因为第一个参数是显示的文本,第二个参数是显示的文本是价值。喜欢这个。

expArry.Add(new ListItem(reader["ID"].ToString(), reader["TITLE"].ToString()));

In which case you should consider using a List<ListItem> instead of ArrayList

在这种情况下,您应该考虑使用List 而不是ArrayList

#3


0  

you could use BinarySearch method if you are searching for value types; in your case this does not seem possible.

如果要搜索值类型,可以使用BinarySearch方法;在你的情况下,这似乎不可能。

I think you may need to use a loop assuming that you can not use Linq (because of the framework employed);

我认为您可能需要使用一个循环,假设您不能使用Linq(因为使用了框架);

int index = -1;
for(int i=0; i<expArray.Count;i++)
{
    ListItem temp = (expArray[i] as ListItem);
    if(temp != null && temp.Value == "some value")
    {
        index = i;
        break;
    }
}

#4


0  

Don't use ArrayList, do something like this using a generic dictionary

不要使用ArrayList,使用通用字典执行类似的操作

public IDictionary<int, string> GetExpenseTypes()
{
    var result = new Dictionary<int, string>();

    using (var conn = new SqlConnection(connStr))
    {
        var getExpenses = new SqlCommand(
            "SELECT ID, TITLE FROM EXPENSE_TYPE",
            conn);

            conn.Open();
            using (var reader = command.ExecureReader())
            {
                while (reader.Read())
                {
                    result.Add(reader.GetInt32(0), reader.GetString(1));
                }
            }
    }

    return result;
}

Then you could look up your string like this

然后你可以像这样查找你的字符串

var type4 = GetExpenseTypes()[4];

alternatively, don't get the whole list to find one value.

或者,不要让整个列表找到一个值。

public string GetExpenseType(int id)
{

    using (var conn = new SqlConnection(connStr))
    {
        var getExpenseType = new SqlCommand(
            "SELECT TITLE FROM EXPENSE_TYPE WHERE ID = @p0",
            conn);

        getExpenseType.Parameters.Add(id);

        conn.Open();
        return (string)getExpenseType.ExecuteScalar();
    }
}

If, for some bad reason, you want to stick with your ArrayList returning function, you can find your item like this.

如果由于某种不好的原因,你想坚持使用ArrayList返回功能,你可以找到这样的项目。

var expenseTypes = GetExpenseTypes().OfType<ListItem>().ToDictionary(
    li => int.Parse(li.Text),
    li => li.Value);

var type4 = expenseTypes[4];

or, if you want to do this once.

或者,如果你想这样做一次。

var type4 = GetExpenseTypes().OfType<ListItem>()
                .Single(li => li.Text == "4").Value;

#1


0  

I'd recommend first changing ArrayList to List<ListItem>. If you can't do that for some reason you could use (assuming each item is unique):

我建议先将ArrayList更改为List 。如果由于某种原因你不能这样做,你可以使用(假设每个项目都是唯一的):

var x = expArry.Cast<ListItem>().SingleOrDefault(exp => exp.Value == value);
if (x != null)
    //item was found

If you know the item will always be there just use Single if it will only be there once.

如果您知道该项目将始终存在,只需使用单一,如果它只会在那里一次。

#2


1  

Instead of ArrayList you might want to use Dictionary<string,string> like this.

您可能希望像这样使用Dictionary 而不是ArrayList。 ,string>

public IDictionary<string,string> GetExpenseTypes()
{
    Dictionary<string,string> expArry = new Dictionary<string,string>();

    // Your sql code

    while (reader.Read())
    {
        expArry.Add(reader["TITLE"].ToString(), reader["ID"].ToString());
    }

    // The rest of your code
}

Then you can get the value like this

然后你可以得到这样的价值

var values = GetExpenseTypes();
string valueYouWant = values["4"];

If on the other hand your problem is that when you use the ListItems in a web control you are seeing the wrong values, then you need to swap the parameters when you create the ListItem because the first parameter is the text that is displayed and the second is the value. Like this.

另一方面,如果您在Web控件中使用ListItems时看到错误的值,那么在创建ListItem时需要交换参数,因为第一个参数是显示的文本,第二个参数是显示的文本是价值。喜欢这个。

expArry.Add(new ListItem(reader["ID"].ToString(), reader["TITLE"].ToString()));

In which case you should consider using a List<ListItem> instead of ArrayList

在这种情况下,您应该考虑使用List 而不是ArrayList

#3


0  

you could use BinarySearch method if you are searching for value types; in your case this does not seem possible.

如果要搜索值类型,可以使用BinarySearch方法;在你的情况下,这似乎不可能。

I think you may need to use a loop assuming that you can not use Linq (because of the framework employed);

我认为您可能需要使用一个循环,假设您不能使用Linq(因为使用了框架);

int index = -1;
for(int i=0; i<expArray.Count;i++)
{
    ListItem temp = (expArray[i] as ListItem);
    if(temp != null && temp.Value == "some value")
    {
        index = i;
        break;
    }
}

#4


0  

Don't use ArrayList, do something like this using a generic dictionary

不要使用ArrayList,使用通用字典执行类似的操作

public IDictionary<int, string> GetExpenseTypes()
{
    var result = new Dictionary<int, string>();

    using (var conn = new SqlConnection(connStr))
    {
        var getExpenses = new SqlCommand(
            "SELECT ID, TITLE FROM EXPENSE_TYPE",
            conn);

            conn.Open();
            using (var reader = command.ExecureReader())
            {
                while (reader.Read())
                {
                    result.Add(reader.GetInt32(0), reader.GetString(1));
                }
            }
    }

    return result;
}

Then you could look up your string like this

然后你可以像这样查找你的字符串

var type4 = GetExpenseTypes()[4];

alternatively, don't get the whole list to find one value.

或者,不要让整个列表找到一个值。

public string GetExpenseType(int id)
{

    using (var conn = new SqlConnection(connStr))
    {
        var getExpenseType = new SqlCommand(
            "SELECT TITLE FROM EXPENSE_TYPE WHERE ID = @p0",
            conn);

        getExpenseType.Parameters.Add(id);

        conn.Open();
        return (string)getExpenseType.ExecuteScalar();
    }
}

If, for some bad reason, you want to stick with your ArrayList returning function, you can find your item like this.

如果由于某种不好的原因,你想坚持使用ArrayList返回功能,你可以找到这样的项目。

var expenseTypes = GetExpenseTypes().OfType<ListItem>().ToDictionary(
    li => int.Parse(li.Text),
    li => li.Value);

var type4 = expenseTypes[4];

or, if you want to do this once.

或者,如果你想这样做一次。

var type4 = GetExpenseTypes().OfType<ListItem>()
                .Single(li => li.Text == "4").Value;