C# 从列表中获取值<;实体类>;

C# 从列表中获取值<;实体类>;,c#,list,C#,List,我正在填充我的列表,如下所示: public List<Functions> ListFunctions(int proj) { List<Functions> lFunctions = new List<Functions>(); try { string sql = "SELECT id, description FROM functios WHERE project = @proje

我正在填充我的
列表,如下所示:

public List<Functions> ListFunctions(int proj)
   {
      List<Functions> lFunctions = new List<Functions>();
      try
         {
            string sql = "SELECT id, description FROM functios WHERE project = @project ORDER BY description ASC";
            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(new MySqlParameter("@project", MySqlDbType.Int32)).Value = proj;

            using (MySqlDataReader reader = _dal.ExecutaReader(cmd))
               {
                  while (reader.Read())
                     {
                        lFunctions.Add(new Function
                          {
                            ID = Convert.ToInt32(reader["id"]),
                            Descripton = reader["description"].ToString()
                          });
                      }
                }
            return lFunction;
         }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }  

您只需枚举返回的列表。例如:

var functions=ListFunctions(0); // For example
foreach(var function in functions)
{
  Console.WriteLine("{0} = {1}",function.ID, function.Description);
}

您只需枚举返回的列表。例如:

var functions=ListFunctions(0); // For example
foreach(var function in functions)
{
  Console.WriteLine("{0} = {1}",function.ID, function.Description);
}

我不熟悉你调用的所有函数。。但是我看不出有什么理由不能使用foreach。使用
foreach
-循环怎么样?@DominicKexel我想过
foreach
我只是不知道如何使用foreach来获得准确的值,你知道吗?获取
Description
的值和
ID
@Jordy Agree。。。我只是不知道如何将
foreach
循环与
list
@Ghaleon一起使用。请尝试在
msdn foreach循环上搜索
。。但是我看不出有什么理由不能使用foreach。使用
foreach
-循环怎么样?@DominicKexel我想过
foreach
我只是不知道如何使用foreach来获得准确的值,你知道吗?获取
Description
的值和
ID
@Jordy Agree。。。我只是不知道如何使用
foreach
循环和
list
@Ghaleon尝试在
msdn foreach循环上搜索
?像
string descripp=listFunction[“fieldName”]
@Ghleon:你必须将它们提取到字典中,并通过字段名查找来访问它们。如果我有两个以上的字段,我如何使用
字段名来循环?像
string descripp=listFunction[“fieldName”]
@Ghleon:您必须将它们提取到字典中,并通过字段名查找来访问它们。