使用 for 循环获取数据表到元组列表

所以我从数据库中获取数据,我需要元组列表中的数据,但是我无法将数据放入元组中以将其放入列表中。


List<Tuple<string,decimal,char>> results = new List< Tuple<string, decimal, char>>() ;


        DataTable table = new DataTable();


        OpenDBConnection.


        using (var comm = connection.CreateCommand())

        {

            GETDATA();

        }




        foreach (DataColumn col in table.Columns)

        {

            object value = table.Rows[0].ItemArray[col.Ordinal];


        }


        CloseConnection()


        return results;

一些我需要如何将值放入元组然后放入列表但是我还没有在网上找到一种方法来做这样的事情。


米脂
浏览 232回答 1
1回答

郎朗坤

这是一个数据示例:DataTable table = new DataTable();table.Columns.Add("C1",typeof(string));table.Columns.Add("C2",typeof(decimal));table.Columns.Add("C3",typeof(char));for(int i = 65; i < 65 + 10; i++){&nbsp; &nbsp; var row = table.NewRow();&nbsp; &nbsp; row[0] = "string"+i.ToString();&nbsp; &nbsp; row[1] = i;&nbsp; &nbsp; row[2] = (char)i;&nbsp; &nbsp; table.Rows.Add(row);}List<Tuple<string, decimal, char>> results = new List< Tuple<string, decimal, char>>();foreach (DataRow r in table.Rows){&nbsp; &nbsp; var tup = Tuple.Create((string)r[0], (decimal)r[1], (char)r[2]);&nbsp; &nbsp; results.Add(tup);}结果:Item1&nbsp; &nbsp; Item2&nbsp; Item3string65&nbsp; &nbsp; 65&nbsp; Astring66&nbsp; &nbsp; 66&nbsp; Bstring67&nbsp; &nbsp; 67&nbsp; Cstring68&nbsp; &nbsp; 68&nbsp; Dstring69&nbsp; &nbsp; 69&nbsp; Estring70&nbsp; &nbsp; 70&nbsp; Fstring71&nbsp; &nbsp; 71&nbsp; Gstring72&nbsp; &nbsp; 72&nbsp; Hstring73&nbsp; &nbsp; 73&nbsp; Istring74&nbsp; &nbsp; 74&nbsp; J
打开App,查看更多内容
随时随地看视频慕课网APP