C# 将字符串列表附加到组合框

C# 将字符串列表附加到组合框,c#,sql,sql-server,C#,Sql,Sql Server,所以我有一个列表,其中typeT是整数。现在我从一个函数中检索这个列表,该函数使用SqlDataReader读取我的数据库记录(DBMS:sqlserver)。基本上,我想做的是检查列表是否返回以下内容: 1 7 4 我想与字符串列表(列表结果=新列表())进行比较,其中: 最后将这些字符串添加到列表中,然后将它们绑定到我的C#Windows窗体应用程序中的ComboBox控件 这种方法的问题是,我使用if条件来检查初始列表中是否存在整数(我从数据库中检索到的),在这种情况下,我们可以将其称为

所以我有一个
列表,其中type
T
是整数。现在我从一个函数中检索这个列表,该函数使用
SqlDataReader
读取我的数据库记录(DBMS:sqlserver)。基本上,我想做的是检查列表是否返回以下内容:

1
7
4
我想与字符串列表(
列表结果=新列表()
)进行比较,其中:

最后将这些字符串添加到列表中,然后将它们绑定到我的C#Windows窗体应用程序中的ComboBox控件

这种方法的问题是,我使用
if条件
来检查初始列表中是否存在整数(我从数据库中检索到的),在这种情况下,我们可以将其称为
list checkInt=new list()
),如果存在,则添加字符串(
香蕉、苹果、桔子或其他任何
)转到
结果
列表

以下是我的实际代码:

  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;
        }
现在的问题是,一旦它执行,编译器将检查if条件,如果它为true,它将只添加一个结果并完成执行(这是if条件正确的执行方式)。但是我想让它检查所有
.Contains(int)
,如果这些比较符合要求,那么只在最后添加结果。我已经知道问题是什么了,我使用了if条件。在将上述内容纳入认知之后,我如何才能得到我想要的结果


注意:由于时间限制,我没有修改我的实际代码以匹配我给出的水果示例,但您肯定会得到我试图实现的结果。

您可以使用
字典来实现这一点:

    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;
    }

除非我使用Linq
var outcome=subgroups.Select(f=>fruit[f]).ToList(),否则我会说什么并在声明
字典水果=新字典{{{1,“苹果”}、{4,“香蕉”}、{7,“橙子”}上初始化字典
  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;
        }
    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;
    }
    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;
    }