-2

I need a little help. I have a one Dataset, you can see on image and I want to copy some rows in Other dataset to use it further. For that we can use Dataset2 = Dataset1.clone() function to get the dataset structure but how can we copy the rows? enter image description here

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Does this answer your question? [Copy rows from one Datatable to another DataTable?](https://stackoverflow.com/questions/4020270/copy-rows-from-one-datatable-to-another-datatable) – mcutrin Sep 14 '21 at 06:14
  • 1
    `DataSets` don't have rows. A `DataSet` contains `DataTables` and, optionally, `DataRelations`. A `DataTable` contains `DataColumns` and `DataRows`. If you are only using a single `DataTable`, as most people do a lot of the time, then you probably shouldn't be using a `DataSet` at all. Unless you specifically need the `DataSet`, just use a `DataTable` on its own. – jmcilhinney Sep 14 '21 at 06:34
  • Actually I could use datatables but as per the application ORM code, it is only dataset is used to fetch the data by provide query, then we manually fill the objects with the data. I actually tried to get the row of data using dataRow from first dataset and tried to add using dataset2.tables(0).add(row) but it is for obvious reason saying cannot find table(0) due to this I've had this idea to copy the rows – Pratik Bilonikar Sep 15 '21 at 07:25
  • It isn't "only dataset". Like I said, a `DataSet` exists as a container for `DataTables` so you're using `DataTables` regardless. What do you think `dataset2.tables(0)` is? It's a `DataTable`. The only question is whether you create a `DataTable` directly, without a containing `DataSet`, or you create one within a `DataSet`. If your data access code requires you to do the latter then so be it, but that likely means that you have poorly-written data access code. There can certainly be reasons to use `DataSets` but they are often used when they shouldn't be. – jmcilhinney Sep 15 '21 at 07:49
  • Note that, if you are wedded to using a `DataSet`, its `Tables` property has an `Add` method, so you can always create a new, filtered `DataTable` and then add it to the same `DataSet` or a completely new `DataSet`. – jmcilhinney Sep 15 '21 at 07:51
  • Oh ok alright. Thank you so much for this precise explanation. Its quite fine explanation to get me the idea to solve the issue, I will try your Note, I think it will be helpful solving my problem. – Pratik Bilonikar Sep 16 '21 at 09:02

1 Answers1

1

The simplest option is to use LINQ to DataSet, e.g.

Dim newTable = oldTable.Select(Function(dr) dr.Field(Of Integer)("Count") > 10).CopyToDataTable()

That will create a new DataTable with the same schema as the old one and copy all the rows where the Count column is greater than 10. You can use whatever condition(s) you want in that lambda expression. Note that you will have to have referenced the appropriate assembly and imported the appropriate namespace. The namespace (System.Data) is probably covered automatically but the assembly/package may or may not be. For WinForms and .NET Framework, it likely will be. For .NET 5, likely not, so you'd need to add the appropriate package.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • I will try this for sure. Since the backend code is in vb.net I need to check how can we manage this syntax. thank you for your answer – Pratik Bilonikar Sep 15 '21 at 07:27
  • What I provided is VB.NET, so there is no issue. That said, if your question is about VB.NET then why did you tag the question C#? Tags are not for your convenience. They are for those answering the question. if the question not about C# then don't tag it C#. – jmcilhinney Sep 15 '21 at 07:46
  • Yes I missed it. Thank you for your suggestion. actually this was first time I have posted question on stackoverflow. anyways, yeah I will try this. – Pratik Bilonikar Sep 16 '21 at 08:58