问题描述
我有一个具有4列(Name
,Contact
,Address
,Marks
)的DataTable
.我想将我的DataTable
转换为List<KeyValuePair<String, int>>
.我想在其中添加Name
和Marks
.我当时在看这个问题,但是我没有找到确切的答案.
I have a DataTable
with 4 Columns (Name
, Contact
, Address
, Marks
). I want to convert my DataTable
to a List<KeyValuePair<String, int>>
. I would like to add Name
and Marks
in that. I was looking at this question but there is no exact answer to what I'm after.
有人可以告诉我如何在不使用Linq的情况下转换数据表.该列表可能有重复的记录,因此我正在考虑使用Tuple.如果可以的话,请帮我举一些例子.
Can someone please tell me how I could convert the data table without using Linq. The List could have duplicate records so I am thinking to use Tuple. If it is possible with that then please help me with some examples.
推荐答案
这应该可以做到. dt当然是数据表的名称.您可能还想做dr ["ColName"]而不是dr [0]:)
This should do it. dt is of course the name of your datatable. You might also want to do dr["ColName"] instead of dr[0] :)
List<KeyValuePair<String, int>> test = new List<KeyValuePair<string, int>>();
foreach (DataRow dr in dt.Rows)
{
test.Add(new KeyValuePair<string, int>(dr[0].ToString(), Convert.ToInt16(dr[3])));
}
这篇关于如何将DataTable转换为List< KeyValuePair< string,int>>.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!