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:您必须将它们提取到字典中,并通过字段名查找来访问它们。