how to select first several datarow from a dataset, and how to create a dataset from datarow?
-
I need to implement the following: I have datatable, from which a dataset is created by selecting all rows. after sorting the dataset based on one column, then I want to get first 5 rows and create a new dataset with the top 5 rows, how to do? I have tried several methods, not successful. thanks for any hint.
-
I need to implement the following: I have datatable, from which a dataset is created by selecting all rows. after sorting the dataset based on one column, then I want to get first 5 rows and create a new dataset with the top 5 rows, how to do? I have tried several methods, not successful. thanks for any hint.
First of all, let me clear your concept about DataSet, DataTable and DataRow. 1. DataSet ( It is a collection of DataTables) 2. DataTable (It is a collection of DataRow) 3. DataRow (It is a collection of Data Elements) Well, Now if you have a datatable, you cant create a DataSet from it... as basically it is already a part of a dataset. If you are talking about coping some rows from one DataTable to a new DataTable, this can be done. Just I am going to create an example for you below :
DataTable dtNew = dt.Clone();
var dataSelect = (from r in dt.AsEnumerable()
orderby r[0]
select r).Take(5);
foreach (DataRow dr in dataSelect)
{
DataRow newRow = dtNew.NewRow();
for(int i=0; iAfter the foreach loop the DAtaTable dtNew will hold only the 5 elements selected in dataSelect.
Hope you got the point. Just mold this into your requirement.Cheers :cool:
Abhishek Sur
My Latest Articles
**Create CLR objects in SQL Server 2005
C# Uncommon Keywords
Read/Write Excel using OleDB**Don't forget to click "Good Answer" if you like to.
-
First of all, let me clear your concept about DataSet, DataTable and DataRow. 1. DataSet ( It is a collection of DataTables) 2. DataTable (It is a collection of DataRow) 3. DataRow (It is a collection of Data Elements) Well, Now if you have a datatable, you cant create a DataSet from it... as basically it is already a part of a dataset. If you are talking about coping some rows from one DataTable to a new DataTable, this can be done. Just I am going to create an example for you below :
DataTable dtNew = dt.Clone();
var dataSelect = (from r in dt.AsEnumerable()
orderby r[0]
select r).Take(5);
foreach (DataRow dr in dataSelect)
{
DataRow newRow = dtNew.NewRow();
for(int i=0; iAfter the foreach loop the DAtaTable dtNew will hold only the 5 elements selected in dataSelect.
Hope you got the point. Just mold this into your requirement.Cheers :cool:
Abhishek Sur
My Latest Articles
**Create CLR objects in SQL Server 2005
C# Uncommon Keywords
Read/Write Excel using OleDB**Don't forget to click "Good Answer" if you like to.
thanks, but .take can be used for ADO.net?
-
thanks, but .take can be used for ADO.net?
yes.. Actually Take comes as Extension Function to all Enumerable objects. You can use them easily. Only thing is to add
System.Linq
namespace to your project. The Static class Enumerable defines all those Extension Methods to IEnumerable objects. :thumbsup::thumbsup: Cheers..Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.