M. X
M. X

Reputation: 1347

Get a part of an C# dataset and c opy it into another dataset

I have a dataset with one datatable. Now I want to set a filter on this table and copy the result to a dataset of the same type, so that I have a new dataset with only the result of the filtering. How can I do this?

Does the following code work?

ds.Tables[<table name>].DefaultView.RowFilter = "ProductId=5";
newDS = ds.copy();

Upvotes: 0

Views: 187

Answers (1)

Dimitri
Dimitri

Reputation: 7013

You could do the following:

DataSet filterResult = new DataSet();
filterResult.Merge(inputDataset.Tables[0].AsEnumerable().Where(r => r["Property1"].ToString().Equals("SomeFilter") || r["Property2"].ToString().Equals("Some other filter")).ToArray());

Please note that DataTable offers Select method, but it throws some very weird exceptions from time to time, hence I prefer working with enumerable list of rows and lambdas instead.

Upvotes: 2

Related Questions