Harold_Finch
Harold_Finch

Reputation: 722

Append List of Strings to ComboBox

So I have a List<T> where type T is integers. Now I am retrieving this list from a function that uses a SqlDataReader to read my database records (DBMS: Sql Server). Essentially what I want to do is check that if the list say, returns the following:

1
7
4

I want to make a comparison to a string list (List <string> outcome = new List<string>()) where:

1 = apples
7 = bananas
4 = oranges

And finally add these strings to a list then bind them to a ComboBox control in my C# Windows Forms Application.

The problem with approach is that I am using an if-condition to check if the integer (which I am retrieving from the database) exists in the initial list which in this case we can call List<int> checkInt = new List<int>()) exists and if it is, add the string (Banana, apple, oranges or whatever) to the outcome list.

Below is my actual code:

  public List<int> getSubGroupsBelongingToUser()
        {
            List<int> DepartmentSubGroupIds = new List<int>();

            myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (mySQLConnection = new SqlConnection(myConnectionString))
            {
                SqlParameter parameter = new SqlParameter("@UserId", getUserID(cbxSelectUser.Text));
                mySQLCommand = new SqlCommand("Test", mySQLConnection);
                mySQLCommand.CommandType = CommandType.StoredProcedure;
                mySQLCommand.Parameters.Add(parameter);
                mySQLConnection.ConnectionString = myConnectionString;
                mySQLConnection.Open();

                SqlDataReader sqlDataReader = mySQLCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    DepartmentSubGroupIds.Add(Convert.ToInt32(sqlDataReader["DepartmentSubGroupId"]));
                }
            }
            return DepartmentSubGroupIds;
        }

So the function above will return 1 and 3. And I call it as below:

  private List<string> getSubGroupPerID()
        {
            List<string> outcome = new List<string>();

            if (getSubGroupsBelongingToUser().Contains(1))
            {
                outcome.Add("Apple");
            }
            else if (getSubGroupsBelongingToUser().Contains(2))
            {
                outcome.Add("Oranges");
            }
            else if (getSubGroupsBelongingToUser().Contains(3))
            {
                outcome.Add("Pineapples");
            }           
            else
            {
                outcome.Add("All");
            }
            return outcome;
        }

Now the problem is that, once it executes, the compiler will check the if-condition and if its true, it will only add that one fruit and finish execution (which is correctly how an if-condition gets executed). But I want it to check all the .Contains(int) and if those comparisons meet then add the fruits only at the end. I already know what the problem is, me using an if-condition. How can I get the result that I want after taking the above into cognisance?

Note: Due to time constraints, I didn't alter my actual code to match the fruit example I gave but surely you should get what I am trying to accomplish.

Upvotes: 2

Views: 141

Answers (1)

Romano Zumb&#233;
Romano Zumb&#233;

Reputation: 8099

You could use a Dictionary for that:

    Dictionary<int, string> fruit = new Dictionary<int, string>();

    fruit.Add(1, "Apple");
    fruit.Add(2, "Oranges");
    fruit.Add(3, "Pineapple");

    private List<string> getSubGroupPerID()
    {
        List<string> outcome = new List<string>();
        List<int> keys = getSubGroupsBelongingToUser();

        if(keys.Count > 0)
        {
            foreach(int key in keys)
            {
                outcome.Add(fruit[key]);
            }
        }      
        else
        {
            outcome.Add("All");
        }

        return outcome;
    }

If you prefer the pattern you were using, the problem is that you if-else there. That means, if the first condition was true the others won't be checked. You can solve this by only using if and then check if anything was added:

    private List<string> getSubGroupPerID()
    {
        List<string> outcome = new List<string>();

        if (getSubGroupsBelongingToUser().Contains(1))
        {
            outcome.Add("Apple");
        }
        if (getSubGroupsBelongingToUser().Contains(2))
        {
            outcome.Add("Oranges");
        }
        if (getSubGroupsBelongingToUser().Contains(3))
        {
            outcome.Add("Pineapples");
        }           
        if(outcome.Count == 0)
        {
            outcome.Add("All");
        }
        return outcome;
    }

Upvotes: 4

Related Questions