Question

I have a List and a ListItemCollection and want to check if there have the same elements.

First, I fill the ListItemCollection with Text and Value. (After a SQL Select)

ListItemCollection tempListName = new ListItemCollection();
ListItem temp_ListItem;

    if (reader.HasRows)
    {
       while (reader.Read())
       {
          temp_ListItem = new ListItem(reader[1].ToString(), reader[0].ToString());
          tempListName.Add(temp_ListItem);
       }
    }

and I have the List

List<string> tempList = new List<string>(ProfileArray);

with some values like {"1","4","5","7"}

now, I want to check, if the tempList have maybe some elements with the same value in tempListName and read the text from the value adn write it in a new list.

Note: Im using asp.net 2.0.

Was it helpful?

Solution 2

Real simple solution that you can customize and optimize as per your needs.

List<string> names = new List<string>(); // This will hold text for matched items found
foreach (ListItem item in tempListName)
{
    foreach (string value in tempList)
    {
        if (value == item.Value)
        {
            names.Add(item.Text);
        }
    }
}

OTHER TIPS

List.FindAll was already available in C# 2.0:

List<string> newList = tempList.FindAll(s => tempListName.FindByText(s) != null);

ListItemCollection.FindByText:

Use the FindByText method to search the collection for a ListItem with a Text property that equals text specified by the text parameter. This method performs a case-sensitive and culture-insensitive comparison. This method does not do partial searches or wildcard searches. If an item is not found in the collection using this criteria, null is returned.

So, for a real simple example, consider something like this:

List<string> tempTextList = new List<string>();

while (reader.Read())
{
    string val = reader[0].ToString(),
        text = reader[1].ToString();

    if (tempList.Contains(val)) { tempTextList.Add(text); }

    temp_ListItem = new ListItem(text, val);
    tempListName.Add(temp_ListItem);
}

Now, just having a listing of the text values doesn't do you much good, so let's improve that a little:

Dictionary<string, string> tempTextList = new Dictionary<string, string>();

while (reader.Read())
{
    string val = reader[0].ToString(),
        text = reader[1].ToString();

    if (tempList.Contains(val)) { tempTextList.Add(val, text); }

    temp_ListItem = new ListItem(text, val);
    tempListName.Add(temp_ListItem);
}

Now you can actually find the text for a specific value from the dictionary. You might even want to declare that Dictionary<string, string> in a higher scope and use it elsewhere. If you were to declare it at a higher scope, you'd just change one line, this:

Dictionary<string, string> tempTextList = new Dictionary<string, string>();

to this:

tempTextList = new Dictionary<string, string>();
        var resultList = new List<string>();
        foreach (string listItem in tempList)
            foreach (ListItem listNameItem in tempListName)
                if (listNameItem.Value.Equals(listItem))
                    resultList.Add(listNameItem.Text);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top