码迷,mamicode.com
首页 > 其他好文 > 详细

一行代码从表中选取N行到另一个表

时间:2015-07-23 13:36:09      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("NUMBER");
            dt.Columns.Add("COUNTRY");
            dt.Rows.Add(1, "PAK");
            dt.Rows.Add(2, "IND");
            dt.Rows.Add(3, "USA");
            dt.Rows.Add(4, "UK");
            dt.Rows.Add(5, "BAN");
            dt.Rows.Add(6, "RSA");
            dt.Rows.Add(7, "CN");
            dt.Rows.Add(8, "AUS");

            // the below function will return top 5 rows of datatable and assign to newly created datatable object
            DataTable dt_Top_N_Rows = Top_N_Rows(dt, 5);
        }

        private DataTable Top_N_Rows(DataTable dt, int NRows)
        {
            return dt.Rows.OfType<DataRow>().ToList().Take(NRows).CopyToDataTable();
        }

一行代码从表中选取N行到另一个表

标签:

原文地址:http://www.cnblogs.com/pengzhihua/p/4670150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!