如何将DataTable转换为List< KeyValuePair< string,int>>. [英] How to convert DataTable to List<KeyValuePair<string, int>>

查看:162
本文介绍了如何将DataTable转换为List< KeyValuePair< string,int>>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有4列(NameContactAddressMarks)的DataTable.我想将我的DataTable转换为List<KeyValuePair<String, int>>.我想在其中添加NameMarks.我当时在看这个问题,但是我没有找到确切的答案.

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&lt; KeyValuePair&lt; string,int&gt;&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆