怎么取出list中的值

hitver 2010-11-13 01:12:09
如题

向list中add了多行信息,怎么取出来?
...全文
1497 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
花满楼 2010-11-13
  • 打赏
  • 举报
回复
list 是单链表
取值:
对象+索引
例如 List<int> a=new List<int>();
for(int i=0;i<100;i++)
{
a.Add(i);
}
int k=a[Index];//Index是要取的元素在集合中的索引。
List的效率没 数组 高。
他们的储存方式不一样。
List 在获取集合的第N个元素值时,必须知道第N-1元素的地址。依次类推。
所以建议在有可能的情况首先选用数组。
hitver 2010-11-13
  • 打赏
  • 举报
回复
UI层

private void test_Click(object sender, EventArgs e)
{
string dbpath = Application.StartupPath+"\\test.mdb";

MDB.BLL.MDBManger mbm = new MDB.BLL.MDBManger();
List<MDB.Model.Remoter> mmr = mbm.dq(dbpath);
//MessageBox.Show(mmr.test1);


richTextBox1.AppendText(mmr[0].ToString());

}

BLL层

public List<MDB.Model.Remoter> dq(string dbpath)
{


MDB.DAL.MDBCity mdm = new MDB.DAL.MDBCity();
List<MDB.Model.Remoter> mmr = mdm.mmbcity(dbpath);

return mmr;


}


DAL层

public List<MDB.Model.Remoter> mmbcity(string mdbpath)
{
OleDbConnection conn = new OleDbConnection(DBconn.MDBconn+mdbpath);
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = @"select * from test order by t_order";
cmd.CommandType = CommandType.Text;
conn.Open();

List<MDB.Model.Remoter> list = new List<MDB.Model.Remoter>();

OleDbDataReader read = cmd.ExecuteReader();
while (read.Read())
{
MDB.Model.Remoter mmr = new MDB.Model.Remoter();
mmr.test1 = read["t_name"].ToString();
list.Add(mmr);
}
return list;


我知道可以用dataguidview
但是我为了能在richtextbox显示
hitver 2010-11-13
  • 打赏
  • 举报
回复

private void test_Click(object sender, EventArgs e)
{
string dbpath = Application.StartupPath+"\\zpw.mdb";

MDB.BLL.MDBManger mbm = new MDB.BLL.MDBManger();
List<MDB.Model.Remoter> mmr = mbm.dq(dbpath);
//MessageBox.Show(mmr.test1);


foreach (var str in mmr)
{
richTextBox1.AppendText(str);
}

}


我的list是一个返回过来的,这样处得好像不对
机器人 2010-11-13
  • 打赏
  • 举报
回复
List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");

foreach(var str in list)
Console.WriteLine(str);

或者

直接访问可以用index。

var str = list[0]; //"1"
aliuwenshen 2010-11-13
  • 打赏
  • 举报
回复
foreach(xx in list)
{
if(xx==?)
{}
}
netharry 2010-11-13
  • 打赏
  • 举报
回复
foreach(xx in list)
{
if(xx==?)
{}
}

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧