1

List像这样填充我的:

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

我现在如何遍历列表并获取这些值?Description和_ID

我想知道一种准确获取字段名称的方法,例如:

string name = ListFunction["FieldName"].ToString()
4

1 回答 1

1

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

var functions=ListFunctions(0); // For example
foreach(var function in functions)
{
  Console.WriteLine("{0} = {1}",function.ID, function.Description);
}
于 2013-07-08T12:44:29.737 回答