Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net中使用循环从列表中读取id_Asp.net_List_C# 4.0_For Loop_Foreach - Fatal编程技术网

如何在asp.net中使用循环从列表中读取id

如何在asp.net中使用循环从列表中读取id,asp.net,list,c#-4.0,for-loop,foreach,Asp.net,List,C# 4.0,For Loop,Foreach,这是我的密码: public class ListData { public int Id { get; set; } public int LinkedUserId { get; set; } } List<ListData> DataList = new List<LinkData>(); using (SqlConnection SqlConnections = new SqlConnection(Global.con)) using (SqlCom

这是我的密码:

public class ListData
{
   public int Id { get; set; }
   public int LinkedUserId { get; set; }
}

List<ListData> DataList = new List<LinkData>();

using (SqlConnection SqlConnections = new SqlConnection(Global.con))
using (SqlCommand SqlCommands = new SqlCommand("SELECT Id,LinkedUserID From Users", SqlConnections))
{
    SqlConnections.Open();

    using (SqlDataReader SqlDataReaders = SqlCommands.ExecuteReader(CommandBehavior.CloseConnection))
    {
        while (SqlDataReaders.Read())
        {
            ListData newItem = new ListData();
            newItem.Id = SqlDataReaders.GetInt32(0);
            newItem.LinkedUserId = SqlDataReaders.GetInt32(1);

            DataList.Add(newItem);
        }

        SqlDataReaders.Close();
   }
公共类ListData
{
公共int Id{get;set;}
public int LinkedUserId{get;set;}
}
List DataList=新列表();
使用(SqlConnection-SqlConnections=newsqlconnection(Global.con))
使用(SqlCommand SqlCommands=new SqlCommand(“从用户中选择Id,LinkedUserID”,SqlConnections))
{
SqlConnections.Open();
使用(SqlDataReader SqlDataReaders=SqlCommands.ExecuteReader(CommandBehavior.CloseConnection))
{
while(SqlDataReaders.Read())
{
ListData newItem=新ListData();
newItem.Id=SqlDataReaders.GetInt32(0);
newItem.LinkedUserId=SqlDataReaders.GetInt32(1);
DataList.Add(newItem);
}
SqlDataReaders.Close();
}
有一个ListData类有两个Id,一个是Id,另一个是LinkedUserId。我想从ListData中读取这两个Id,然后从运行循环中逐个返回。我想执行如下操作:

            for (int i = 0; i < DataList .Count; i++)
            {
                string id = DataList[i].ToString();
            }
for(int i=0;i
有人知道我如何为循环或使用foreach循环返回值表单吗

谢谢你这是你需要的吗

    var result = new int[DataList.Count*2];
    for (int i = 0; i < DataList.Count; i++)
    {
        result[i * 2] = DataList[i].Id;
        result[i * 2 + 1] = DataList[i].LinkedUserId;
    }
var result=newint[DataList.Count*2];
for(int i=0;i
什么是
链接[i]
?显示(
其数据列表[i]而非链接[i]的
的完整代码
foreach (ListData listData in DataList)
{
    string id = Convert.ToString(listData.Id);
    string linkedUserId = Convert.ToString(listData.LinkedUserId);
}